rails 3.0.10 не может добавить приложение sinatra - PullRequest
1 голос
/ 19 августа 2011

В приложении Rails-3.0.10 у меня есть такой маршрут:

  match "/" => GithubTrends

, который указывает приложение sinatra в 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

... но это не работает, получая следующие ошибки:

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$

Первая строка говорит "неинициализированная константа GithubTrends (NameError)" ...

ОБНОВЛЕНИЕ

Я добавил следующее в config / application.rb:

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

... но теперь я получаю:

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:

запустив sinatra вручную, запустите сервер, но получите, «может быть», ошибку маршрутизации:

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 (последнее и рабочее обновление):

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

было недостаточно вмое дело.Я по-прежнему получаю следующую ошибку:

/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)

... также работает ruby lib/github_trends.rb (запускается вручную в изоляции, как подсказывают hrickards ), работает нормально после некоторой «подрезки», нополучил предыдущую ошибку при запуске через "rails s".

Но было достаточно добавить require File.dirname( __FILE__ ) + "/lib/github_trends" в config.ru файл rails, чтобы все работало нормально!

Это было по предложению Райан Бигг , см. Комментарий № 10:

... проблема в том, что Лука не требует файла, который определяет эту константу, прежде чем он попытается обратиться к ней

... даже если я не совсем понимаю, как один config.autoload_paths один не работал ...

Я хотел бы поблагодарить за устранение неполадок и config.autoload_pathsпредложено hrickards см. комментарии.

Спасибо, luca

1 Ответ

1 голос
/ 19 августа 2011

Согласно комментариям выше, нам удалось изолировать проблему до ошибки в реальном приложении Sinatra.

Мой совет: начните с приложения hello world с сайта Sinatra и медленно добавляйте куски кода из своего приложения, пока не найдете перерыв.

...