6

Rails にラック アプリケーションをマウントしようとしていますが、機能しません。私の問題の最も単純なバージョンを再現しました。Ruby 1.9.3p327 と Rails 3.2.11 を使用しています。

を使用して新しい Rails アプリケーションを作成することから始めましたrails new problem_example。problem_example ディレクトリに移動した後、自分の Rails アプリケーションにアクセスできるようにhttp://localhost:3000なり、人生は素晴らしいものになりました。

Rack in Rails 3 Railscastの指示に従って、以下をルートに追加しました。

ProblemExample::Application.routes.draw do
  root :to => proc { |env| [200, {}, ["Oh yeah!"]] }
end

また、パブリック フォルダーから index.html ファイルを削除しました。に移動するとhttp://localhost:3000、「そうそう!」と表示されます。私の画面に出力します。

ここがトリッキーな部分です。ルートを次のように変更すると、次のようになります。

class SimpleRackApplication
  def call(env)
    [200, {'Content-Type' => 'text/plain'}, ["Oh no!"]]
  end
end

ProblemExample::Application.routes.draw do
  root :to => SimpleRackApplication
end

を実行するrails sと、次の厄介なエラーが発生します。

=> Booting WEBrick
=> Rails 3.2.11 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/actionpack-3.2.11/lib/action_dispatch/routing/mapper.rb:181:in `default_controller_and_action': missing :controller (ArgumentError)
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/actionpack-3.2.11/lib/action_dispatch/routing/mapper.rb:78:in `normalize_options!'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/actionpack-3.2.11/lib/action_dispatch/routing/mapper.rb:61:in `initialize'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/actionpack-3.2.11/lib/action_dispatch/routing/mapper.rb:1304:in `new'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/actionpack-3.2.11/lib/action_dispatch/routing/mapper.rb:1304:in `add_route'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/actionpack-3.2.11/lib/action_dispatch/routing/mapper.rb:1284:in `decomposed_match'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/actionpack-3.2.11/lib/action_dispatch/routing/mapper.rb:1270:in `block in match'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/actionpack-3.2.11/lib/action_dispatch/routing/mapper.rb:1270:in `each'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/actionpack-3.2.11/lib/action_dispatch/routing/mapper.rb:1270:in `match'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/actionpack-3.2.11/lib/action_dispatch/routing/mapper.rb:260:in `root'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/actionpack-3.2.11/lib/action_dispatch/routing/mapper.rb:1317:in `root'
    from /Users/landonschropp/Dropbox/Development/current/problem_example/config/routes.rb:9:in `block in <top (required)>'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/actionpack-3.2.11/lib/action_dispatch/routing/route_set.rb:282:in `instance_exec'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/actionpack-3.2.11/lib/action_dispatch/routing/route_set.rb:282:in `eval_block'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/actionpack-3.2.11/lib/action_dispatch/routing/route_set.rb:260:in `draw'
    from /Users/landonschropp/Dropbox/Development/current/problem_example/config/routes.rb:8:in `<top (required)>'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `load'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `block in load'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `load'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/application/routes_reloader.rb:40:in `each'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/application/routes_reloader.rb:40:in `load_paths'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/application/routes_reloader.rb:16:in `reload!'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/application/routes_reloader.rb:26:in `block in updater'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/file_update_checker.rb:78:in `call'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/file_update_checker.rb:78:in `execute'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/application/routes_reloader.rb:27:in `updater'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/application/finisher.rb:66:in `block in <module:Finisher>'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `run'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `each'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `run_initializers'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/application.rb:136:in `initialize!'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /Users/landonschropp/Dropbox/Development/current/problem_example/config/environment.rb:5:in `<top (required)>'
    from /Users/landonschropp/Dropbox/Development/current/problem_example/config.ru:3:in `require'
    from /Users/landonschropp/Dropbox/Development/current/problem_example/config.ru:3:in `block in <main>'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/rack-1.4.4/lib/rack/builder.rb:51:in `instance_eval'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/rack-1.4.4/lib/rack/builder.rb:51:in `initialize'
    from /Users/landonschropp/Dropbox/Development/current/problem_example/config.ru:in `new'
    from /Users/landonschropp/Dropbox/Development/current/problem_example/config.ru:in `<main>'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/rack-1.4.4/lib/rack/builder.rb:40:in `eval'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/rack-1.4.4/lib/rack/builder.rb:40:in `parse_file'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/rack-1.4.4/lib/rack/server.rb:200:in `app'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands/server.rb:46:in `app'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/rack-1.4.4/lib/rack/server.rb:304:in `wrapped_app'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/rack-1.4.4/lib/rack/server.rb:254:in `start'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands/server.rb:70:in `start'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands.rb:55:in `block in <top (required)>'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands.rb:50:in `tap'
    from /Users/landonschropp/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands.rb:50:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

どうしたの?

4

1 に答える 1

14

あぁ。Rack アプリケーションをインスタンス化する必要があります。

root :to => SimpleRackApplication.new
于 2013-01-14T08:29:18.953 に答える