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