rails 3.0.10无法添加sinatra应用程序

时间:2011-08-18 21:32:34

标签: ruby-on-rails ruby-on-rails-3 routing sinatra rack

在Rails-3.0.10应用程序中,我有一条这样的路线:

  match "/" => GithubTrends

指向lib / github_trends.rb中的sinatra应用程序:

$:.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

更新(最后一次和工作更新):

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的建议单独手动运行),在一些'triming'之后工作正常,但在通过“rails s”运行时得到了上一个错误。 / p>

但是将require File.dirname( __FILE__ ) + "/lib/github_trends"添加到config.ru rails文件就足够了,以便工作正常!

这是Ryan Bigg的建议,见评论n.10:

  

......问题是Luca不需要定义它的文件   在他试图引用它之前不变

......即使我完全不了解config.autoload_paths的方式,也没有工作......

我要感谢您的故障排除&amp; hrickards建议的config.autoload_paths查看评论。

由于 卢卡

1 个答案:

答案 0 :(得分:1)

根据上面的评论,我们设法将问题分解为实际的Sinatra应用程序中的错误。

我的建议是从Sinatra网站开始使用hello world应用程序,然后慢慢添加应用程序中的代码片段直到找到休息时间。