ActionController::RoutingError (No route matches [

2019-02-20 03:19发布

问题:

I'm playing around with a tutorial which uses unicorn and rails. I'm completely new to rails and for the purpose of the tutorial all I've done for the project is bundle exec rails new rails-starter with no further app modifications.

When I run bundle exec unicorn -c config/unicorn.rb -E production I get the message in the browser:

The page you were looking for doesn't exist. You may have mistyped the address or the page may have moved.

The log shows:

ActionController::RoutingError (No route matches [GET] "/"):
  /home.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/actionpack-4.0.4/lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  /home.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/actionpack-4.0.4/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  /home.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/railties-4.0.4/lib/rails/rack/logger.rb:38:in `call_app'
  /home.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/railties-4.0.4/lib/rails/rack/logger.rb:20:in `block in call'
  /home.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.4/lib/active_support/tagged_logging.rb:68:in `block in tagged'
  /home.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.4/lib/active_support/tagged_logging.rb:26:in `tagged'
  /home.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.4/lib/active_support/tagged_logging.rb:68:in `tagged'
  /home.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/railties-4.0.4/lib/rails/rack/logger.rb:20:in `call'
  /home.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/actionpack-4.0.4/lib/action_dispatch/middleware/request_id.rb:21:in `call'
  /home.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/methodoverride.rb:21:in `call'
  /home.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/runtime.rb:17:in `call'
  /home.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.4/lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  /home.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/sendfile.rb:112:in `call'
  /home.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/railties-4.0.4/lib/rails/engine.rb:511:in `call'
  /home.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/railties-4.0.4/lib/rails/application.rb:97:in `call'
  /home.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/unicorn-4.8.2/lib/unicorn/http_server.rb:572:in `process_client'
  /home.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/unicorn-4.8.2/lib/unicorn/http_server.rb:666:in `worker_loop'
  /home.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/unicorn-4.8.2/lib/unicorn/http_server.rb:521:in `spawn_missing_workers'
  /home.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/unicorn-4.8.2/lib/unicorn/http_server.rb:140:in `start'
  /home.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/unicorn-4.8.2/bin/unicorn:126:in `<top (required)>'
  /home.rbenv/versions/2.0.0-p0/bin/unicorn:23:in `load'
  /home.rbenv/versions/2.0.0-p0/bin/unicorn:23:in `<main>'

However if I just run the rails app via bundle exec rails server I can successfully access via [IP]:3000

I suspect the error is something to do with ActionController::RoutingError (No route matches [GET] "/"): however I lack the rails knowledge to figure out the fix.

回答1:

You need to setup a root route in routes.rb file.

root :to => 'index#index'

Where the first index is the controller name (IndexController) and the second index is the action name in the IndexController.