2011-07-15 37 views
1

我似乎無法使用Rails 3.1(rc4)的簡單機架中間件。有什麼缺失?以下是我建我的應用程序:Rails 3.1rc4 + Rack:有什麼改變?

gem 'rack-contrib' 

我這些行添加到config/application.rb

require 'rack/contrib' 
... 
config.middleware.use Rack::NotFound.new('public/404.html') 

[注:

$ rails new skel192 -G -O -T 

我在Gemfile末加入這一行我我並沒有真正計劃使用RackNotFound,我試圖使用我能想到的最簡單的中間件。]

那麼好措施:

$ bundle install 
... 
$ bundle update 
... 

FYI:

$ 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

我仍然不知道爲什麼Rack::NotFoundrack-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 

然後更新您的中間件的依賴。

2

您需要創建的Rack::NotFound一個實例,因此在application.rb你需要把:

config.middleware.use Rack::NotFound.new("/path/to/404.html") 
+0

你是對的(upvote!),但它仍然不適合我。我正在更新我的問題以納入您的建議。它似乎仍然是Rack的一些問題。 – jwfearn

0

我不知道這是回答你的問題,但Rails的正式不支持Ruby 1.9.1,只有1.8.7和1.9.2。

編輯:看完路徑後,你使用1.9.1還是1.9.2?寶石似乎安裝在1.9.1

+1

我使用Ruby 1.8.7和1.9.2。我在Linux上使用'rvm',在窗口上使用'pik'來在Rubies之間切換。您在路徑中看到的「1.9.1」只是Ruby 1.9.2如何命名(不知道爲什麼)。如果你安裝1.9.2,你可以自己看。 – jwfearn

+0

當我用gemsets使用rvm時,我沒有在我的寶石路徑中看到1.9.1。這個問題也發生在Linux上?根據您的回答,您是否將Rails與Sinatra結合使用? – theIV

+0

我從來沒有在任何平臺上安裝Ruby 1.9.1。我真的不知道爲什麼這個名字出現在任何路徑中,也許這是Windows特有的東西?是的,我的目標是讓我的Sinatra中間件組件正常工作。但作爲測試,我使用Rails 3.1和Ruby 1.9.2來生成一個框架應用程序,我添加了一個Rack :: NotFound中間件(沒有Sinatra)。我的Q是關於這個簡單的設置。 – jwfearn