我似乎无法使用带有Rails 3.1(rc4)的简单机架中间件。缺少什么?以下是我构建应用的方式:
$ rails new skel192 -G -O -T
我在Gemfile
:
gem 'rack-contrib'
我将这些行添加到config/application.rb
:
require 'rack/contrib'
...
config.middleware.use Rack::NotFound.new('public/404.html')
[注意:我并不打算使用RackNotFound
,我试图使用我能想到的最简单的中间件。]
然后好好衡量:
$ bundle install
...
$ bundle update
...
供参考:
$ ruby -v
ruby 1.9.2p290 (2011-07-09) [i386-mingw32]
$ rails -v
Rails 3.1.0.rc4
但是当我跑步时:
$ rails server
=> Booting WEBrick
=> Rails 3.1.0.rc4 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/inflector/methods.rb:124:in `const_defined?': wrong constant name #<Rack (NameError)
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/inflector/methods.rb:124:in `block in constantize'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/inflector/methods.rb:123:in `each'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/inflector/methods.rb:123:in `constantize'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/dependencies.rb:527:in `block in initialize'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/dependencies.rb:549:in `yield'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/dependencies.rb:549:in `default'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/dependencies.rb:549:in `[]'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.1.0.rc4/lib/action_dispatch/middleware/stack.rb:24:in `klass'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.1.0.rc4/lib/action_dispatch/middleware/stack.rb:43:in `build'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.1.0.rc4/lib/action_dispatch/middleware/stack.rb:112:in `block in build'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.1.0.rc4/lib/action_dispatch/middleware/stack.rb:112:in `each'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.1.0.rc4/lib/action_dispatch/middleware/stack.rb:112:in `inject'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.1.0.rc4/lib/action_dispatch/middleware/stack.rb:112:in `build'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0.rc4/lib/rails/engine.rb:429:in `app'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0.rc4/lib/rails/application/finisher.rb:37:in `block in <module:Finisher>'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0.rc4/lib/rails/initializable.rb:25:in `instance_exec'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0.rc4/lib/rails/initializable.rb:25:in `run'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0.rc4/lib/rails/initializable.rb:50:in `block in run_initializers'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0.rc4/lib/rails/initializable.rb:49:in `each'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0.rc4/lib/rails/initializable.rb:49:in `run_initializers'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0.rc4/lib/rails/application.rb:96:in `initialize!'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0.rc4/lib/rails/railtie/configurable.rb:30:in `method_missing'
from C:/work/tech/external/lb/skel192/config/environment.rb:5:in `<top (required)>'
from C:/work/tech/external/lb/skel192/config.ru:4:in `require'
from C:/work/tech/external/lb/skel192/config.ru:4:in `block in <main>'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.3.1/lib/rack/builder.rb:51:in `instance_eval'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.3.1/lib/rack/builder.rb:51:in `initialize'
from C:/work/tech/external/lb/skel192/config.ru:1:in `new'
from C:/work/tech/external/lb/skel192/config.ru:1:in `<main>'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.3.1/lib/rack/builder.rb:40:in `eval'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.3.1/lib/rack/builder.rb:40:in `parse_file'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.3.1/lib/rack/server.rb:200:in `app'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0.rc4/lib/rails/commands/server.rb:46:in `app'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.3.1/lib/rack/server.rb:301:in `wrapped_app'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.3.1/lib/rack/server.rb:252:in `start'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0.rc4/lib/rails/commands/server.rb:70:in `start'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0.rc4/lib/rails/commands.rb:54:in `block in <top (required)>'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0.rc4/lib/rails/commands.rb:49:in `tap'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0.rc4/lib/rails/commands.rb:49:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
我错过了什么吗?
答案 0 :(得分:2)
您需要创建Rack::NotFound
的实例,因此在application.rb
中您需要放置:
config.middleware.use Rack::NotFound.new("/path/to/404.html")
答案 1 :(得分:0)
我不知道这是否是您问题的答案,但Rails并非正式支持Ruby 1.9.1,仅支持1.8.7和1.9.2。
编辑:看完路径后,你使用的是1.9.1还是1.9.2?宝石似乎安装在1.9.1
中答案 2 :(得分:0)
我仍然不确定为什么Rack::NotFound
和其他rack-contrib
中间件使用Rails 3.1失败但我确实学到了其他东西:
Sinatra 1.2.x does not play well with Rails 3.1。切换到Sinatra 1.3.0.e消除了我使用自己的中间件遇到的一些其他问题。
安装:
$ [sudo] gem install sinatra --prerelease
然后更新中间件的依赖项。