1

In a Rails-3.0.10 app, I've got a route like this:

  match "/" => GithubTrends

which point a sinatra app in lib/github_trends.rb :

$:.unshift File.dirname(__FILE__)

class GithubTrends < Sinatra::Base

disable :run
set :root, File.dirname(__FILE__) + '/../'
set :cache_enabled, true # production

LANGUAGES = ['ActionScript', 'Ada', 'Arc', 'Asp', 'Assembly', 'Boo', 'C', 'C#', 'C++', 'Clojure', 'CoffeeScript', 'ColdFusion', 'Common Lisp', 'D', 'Delphi', 'Dubi', 'Eiffel', 'Emacs Lisp', 'Erlang', 'F#', 'Factor', 'FORTRAN', 'Go', 'Groovy', 'Haskell', 'HaXe', 'Io', 'Java', 'JavaScript', 'Lua', 'Max/MSP', 'Nu', 'Objective-C', 'Objective-J', 'OCaml', 'ooc', 'Perl', 'PHP', 'Pure Data', 'Python', 'R', 'Racket', 'Ruby', 'Scala', 'Scheme', 'sclang', 'Self', 'Shell', 'SmallTalk', 'SuperCollider', 'Tcl', 'Vala', 'Verilog', 'VHDL', 'VimL', 'Visual Basic', 'XQuery']

CONTEXTS = ['most_watched_today', 'most_watched_this_week', 'most_watched_this_month', 'most_watched_overall', 'most_forked_today', 'most_forked_this_week', 'most_forked_this_month', 'most_forked_overall']

get "/" do
  @languages = LANGUAGES
  @contexts = CONTEXTS
  haml(:index)
end

... but it doesn't work, getting the following errors:

lsoave@ubuntu:~/rails/github/gitwatcher$ rails s -p3001
=> Booting Mongrel
=> Rails 3.0.10 application starting in development on http://0.0.0.0:3001
=> Call with -d to detach
=> Ctrl-C to shutdown server
MongoDB logging. Please note that logging negatively impacts performance and should be disabled for high-performance production apps.
MONGODB admin['$cmd'].find({:ismaster=>1})
MONGODB admin['$cmd'].find({:ismaster=>1})
Exiting
/home/lsoave/rails/github/gitwatcher/config/routes.rb:3:in `block in <top (required)>': uninitialized constant GithubTrends (NameError)
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.10/lib/action_dispatch/routing/route_set.rb:233:in `instance_exec'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.10/lib/action_dispatch/routing/route_set.rb:233:in `draw'
    from /home/lsoave/rails/github/gitwatcher/config/routes.rb:1:in `<top (required)>'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/application.rb:127:in `block in reload_routes!'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/application.rb:127:in `each'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/application.rb:127:in `reload_routes!'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/application.rb:120:in `block in routes_reloader'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.10/lib/active_support/file_update_checker.rb:32:in `call'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.10/lib/active_support/file_update_checker.rb:32:in `execute_if_updated'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/application/finisher.rb:51:in `block (2 levels) in <module:Finisher>'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/application/finisher.rb:52:in `call'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/application/finisher.rb:52:in `block in <module:Finisher>'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/initializable.rb:25:in `instance_exec'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/initializable.rb:25:in `run'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/initializable.rb:50:in `block in run_initializers'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/initializable.rb:49:in `each'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/initializable.rb:49:in `run_initializers'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/application.rb:134:in `initialize!'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/application.rb:77:in `method_missing'
    from /home/lsoave/rails/github/gitwatcher/config/environment.rb:5:in `<top (required)>'
    from /home/lsoave/rails/github/gitwatcher/config.ru:3:in `block in <main>'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.3/lib/rack/builder.rb:46:in `instance_eval'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.3/lib/rack/builder.rb:46:in `initialize'
    from /home/lsoave/rails/github/gitwatcher/config.ru:1:in `new'
    from /home/lsoave/rails/github/gitwatcher/config.ru:1:in `<main>'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.3/lib/rack/builder.rb:35:in `eval'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.3/lib/rack/builder.rb:35:in `parse_file'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.3/lib/rack/server.rb:162:in `app'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.3/lib/rack/server.rb:253:in `wrapped_app'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.3/lib/rack/server.rb:204:in `start'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/commands/server.rb:65:in `start'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/commands.rb:30:in `block in <top (required)>'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/commands.rb:27:in `tap'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/commands.rb:27:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
lsoave@ubuntu:~/rails/github/gitwatcher$

The fist line say "uninitialized constant GithubTrends (NameError)" ...

UPDATE

I added the following to config/application.rb:

config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]

... but now I get :

Exiting
/home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/application.rb:168:in `call': undefined method `reverse_merge!' for Sinatra::Application:Class (NoMethodError)
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/application.rb:77:in `method_missing'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.3/lib/rack/builder.rb:73:in `block in to_app'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.3/lib/rack/builder.rb:73:in `each'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.3/lib/rack/builder.rb:73:in `inject'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.3/lib/rack/builder.rb:73:in `to_app'
    from /home/lsoave/rails/github/gitwatcher/config.ru:1:in `<main>'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.3/lib/rack/builder.rb:35:in `eval'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.3/lib/rack/builder.rb:35:in `parse_file'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.3/lib/rack/server.rb:162:in `app'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.3/lib/rack/server.rb:253:in `wrapped_app'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/rack-1.2.3/lib/rack/server.rb:204:in `start'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/commands/server.rb:65:in `start'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/commands.rb:30:in `block in <top (required)>'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/commands.rb:27:in `tap'
    from /home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/commands.rb:27:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

UPDATE-2:

running sinatra manually, start the server but get, 'may be', a routing error :

lsoave@ubuntu:~/rails/github/gitwatcher$ ruby lib/github_trends.rb
== Sinatra/1.2.6 has taken the stage on 4567 for development with backup from Thin
>> Thin web server (v1.2.11 codename Bat-Shit Crazy)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:4567, CTRL+C to stop
^T127.0.0.1 - - [19/Aug/2011 00:18:50] "GET / HTTP/1.1" 404 436 0.0011
127.0.0.1 - - [19/Aug/2011 00:18:50] "GET /__sinatra__/404.png HTTP/1.1" 200 23305 0.0327
127.0.0.1 - - [19/Aug/2011 00:18:51] "GET /favicon.ico HTTP/1.1" 404 447 0.0010

UPDATE (the last & working update):

config.autoload_paths += %W(#{config.root}/lib) 

was not enough in my case. I still got the following error :

/home/lsoave/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.10/lib/rails/application.rb:168:in `call': undefined method `reverse_merge!' for Sinatra::Application:Class (NoMethodError)

... also running ruby lib/github_trends.rb (manually ran in isolation as suggested by hrickards ), worked fine after some 'triming', but got the previous error when running via "rails s".

But it was enough adding require File.dirname( __FILE__ ) + "/lib/github_trends" to config.ru rails file, to have everithing working fine !

This was on suggestion by Ryan Bigg, see comment n.10:

... the problem is that Luca is not requiring the file that defines that constant before he attempts to reference it

... even if, I don't fully understand way config.autoload_paths alone, didn't work ...

I'd like to thanks for the troubleshooting & config.autoload_paths suggested by hrickards see the comments.

Thanks luca

Community
  • 1
  • 1
Luca G. Soave
  • 12,271
  • 12
  • 58
  • 109
  • Have you enabled the inclusion of files in the lib/ directory in config/application.rb / manually required lib/github_trends.rb? – hrickards Aug 18 '11 at 21:45
  • On a second look, Rails should be automatically loading the file because it's named after the GithubTrends class. However, you could try loading all files in the lib/ directory anyway - take a look at http://stackoverflow.com/questions/3356742/best-way-to-load-module-class-from-lib-folder-in-rails-3 – hrickards Aug 18 '11 at 22:09
  • Ok. Do have Sinatra in your Gemfile. It would probably also be wise to add a require 'sinatra' at the top of your github_trends.rb. Perhaps also try running Sinatra directly - e.g 'ruby lib/github_trends.rb' – hrickards Aug 18 '11 at 22:15
  • done, I get something but not right routed ( see UPDATE-2 ) ... – Luca G. Soave Aug 18 '11 at 22:23
  • Ok, it seems your actual Sinatra app has a bug in then. I can't immediately spot anything, but I'd suggest starting with the hello world app from the Sinatra website, and slowly adding pieces of yours until you identify the break. – hrickards Aug 18 '11 at 22:32
  • ... ok, I'll take your suggestion! Many thanks until now, ... If you'd like to post the previous as an answer, I'll be happy to give you back some ++ votes :-) – Luca G. Soave Aug 18 '11 at 22:41
  • Will do :) Let us know when you find the bug. – hrickards Aug 18 '11 at 22:56
  • @hrickards: Files are not automatically loaded by Rails from the `lib` directory unless `config.autoload_paths` is configured to contain that directory. Hint: it's not by default. The problem is that Luca is not requiring the file that defines that constant before he attempts to reference it. – Ryan Bigg Aug 18 '11 at 23:37
  • Ryan: Lucas already added the lib (apologies for no formatting in all of those comments: I can't seem to find the backtick on an iDevice) directory to config.autoload_paths, as per the updates in the OP. He is getting an error even when running Sinatra directly without Rails - suggesting to me that there is a problem with the Sinatra app, rather than the Rails code calling it. – hrickards Aug 19 '11 at 00:24
  • @Ryan: thanks for your suggestion. I had config.autoload_paths in place, but still not requiring the file that defines that constant before he attempts to reference it. See: UPDATE (the last & working update) – Luca G. Soave Aug 19 '11 at 22:51
  • @hrickards: thanks for your support, the link to previous post and the way to isolate the issue :-) – Luca G. Soave Aug 19 '11 at 22:55

1 Answers1

1

As per the comments above, we managed to isolate the issue down to a bug in the actual Sinatra app.

My advice would be to start with the hello world app from the Sinatra site, and slowly add in pieces of code from your app until you find the break.

hrickards
  • 1,061
  • 2
  • 9
  • 20