为什么我的开发服务器无法加载?default_controller_and_action': missing :action (ArgumentError)

27

我是一个Ruby初学者(也是Stack Overflow的新手),正在使用Michael Hartl的Rails教程,突然间我的开发服务器无法加载并且一直崩溃。控制台也无法加载,而且Rspec spec/会产生错误/异常。 我在下面包含了一些终端输出,非常感谢您的帮助。

Exiting
/Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/actionpack-3.0.3/lib/action_dispatch/routing/mapper.rb:164:in `default_controller_and_action': missing :action (ArgumentError)
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/actionpack-3.0.3/lib/action_dispatch/routing/mapper.rb:68:in `normalize_options!'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/actionpack-3.0.3/lib/action_dispatch/routing/mapper.rb:51:in `initialize'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/actionpack-3.0.3/lib/action_dispatch/routing/mapper.rb:261:in `new'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/actionpack-3.0.3/lib/action_dispatch/routing/mapper.rb:261:in `match'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/actionpack-3.0.3/lib/action_dispatch/routing/mapper.rb:1112:in `match'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/actionpack-3.0.3/lib/action_dispatch/routing/mapper.rb:1297:in `match'
    from /Users/ameedachowdhury/rails_projects/railstutorial/config/routes.rb:11:in `block in <top (required)>'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/actionpack-3.0.3/lib/action_dispatch/routing/route_set.rb:233:in `instance_exec'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/actionpack-3.0.3/lib/action_dispatch/routing/route_set.rb:233:in `draw'
    from /Users/ameedachowdhury/rails_projects/railstutorial/config/routes.rb:1:in `<top (required)>'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:235:in `load'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:235:in `block in load'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `block in load_dependency'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:596:in `new_constants_in'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `load_dependency'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:235:in `load'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/railties-3.0.3/lib/rails/application.rb:127:in `block in reload_routes!'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/railties-3.0.3/lib/rails/application.rb:127:in `each'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/railties-3.0.3/lib/rails/application.rb:127:in `reload_routes!'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/railties-3.0.3/lib/rails/application.rb:120:in `block in routes_reloader'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/activesupport-3.0.3/lib/active_support/file_update_checker.rb:32:in `call'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/activesupport-3.0.3/lib/active_support/file_update_checker.rb:32:in `execute_if_updated'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/railties-3.0.3/lib/rails/application/finisher.rb:51:in `block (2 levels) in <module:Finisher>'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/railties-3.0.3/lib/rails/application/finisher.rb:52:in `call'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/railties-3.0.3/lib/rails/application/finisher.rb:52:in `block in <module:Finisher>'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `instance_exec'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `run'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/railties-3.0.3/lib/rails/initializable.rb:50:in `block in run_initializers'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `each'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `run_initializers'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/railties-3.0.3/lib/rails/application.rb:134:in `initialize!'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/railties-3.0.3/lib/rails/application.rb:77:in `method_missing'
    from /Users/ameedachowdhury/rails_projects/railstutorial/config/environment.rb:5:in `<top (required)>'
    from /Users/ameedachowdhury/rails_projects/railstutorial/config.ru:3:in `require'
    from /Users/ameedachowdhury/rails_projects/railstutorial/config.ru:3:in `block in <main>'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/rack-1.2.1/lib/rack/builder.rb:46:in `instance_eval'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/rack-1.2.1/lib/rack/builder.rb:46:in `initialize'
    from /Users/ameedachowdhury/rails_projects/railstutorial/config.ru:1:in `new'
    from /Users/ameedachowdhury/rails_projects/railstutorial/config.ru:1:in `<main>'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/rack-1.2.1/lib/rack/builder.rb:35:in `eval'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/rack-1.2.1/lib/rack/builder.rb:35:in `parse_file'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/rack-1.2.1/lib/rack/server.rb:162:in `app'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/rack-1.2.1/lib/rack/server.rb:248:in `wrapped_app'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/rack-1.2.1/lib/rack/server.rb:213:in `start'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/railties-3.0.3/lib/rails/commands/server.rb:65:in `start'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/railties-3.0.3/lib/rails/commands.rb:30:in `block in <top (required)>'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/railties-3.0.3/lib/rails/commands.rb:27:in `tap'
    from /Users/ameedachowdhury/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/railties-3.0.3/lib/rails/commands.rb:27:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
ameeda-chowdhurys-macbook:railstutorial ameedachowdhury$ 

6个回答

69
当我遇到这个问题时,原因是在routes.rb中的一个路由在使用controller#action语法时,有一个斜杠(/) 而不是哈希符号 (#)。(我使用了 "controller/action",这是错误的。)
例如,我错误地写成:root :to => 'home/index',而应该是root :to => 'home#index'
我在这里找到了解决方案。

3
老问题:使用斜杠而非井号 - Catfish

8
这是由于不正确的路由导致的 -
请检查你的config/routes.rb文件,即使有一个路由设置错误,也会抛出此错误!
还要确保路由是正确的。
'/url/for/something' => 'controller#action'

或者

root :to => 'controller#action

你可能会遇到和我一样的错误,因为我正在跟着同一个教程学习。 只需在routes.rb中注释掉最后修改的原始代码,然后你就能看到哪个路由引起了错误,接着修复语法即可。这就是解决方案,谢谢。 - YogiZoli

2

我也是一个 Ruby 初学者,在学习 Michael Hartl 的 Rails 教程时遇到了同样的错误。如果你和我一样,可能只是在书中输入所有代码,而没有阅读或理解文本。并非每个步骤都是按照顺序进行的。在演示驼峰命名法与下划线命名法时,Hartl 展示了一个带有以下代码的框:

$ rails generate controller static_pages ...

在这里,Hartl只是试图对比使用蛇形命名法生成StaticPages控制器的说明,而不是像3.4清单中所做的那样使用驼峰命名法。省略号“...”仅用于编辑或截断命令行的其余部分。我输入了完全按照所示的内容,并在我的路由文件中得到了以下结果:

SampleApp::Application.routes.draw do
 get "static_pages/..."

 get "static_pages/home"

 get "static_pages/help"

打开你的路由文件,删除。
get "static_pages/..."

保存路由文件,然后尝试重新启动Rails服务器。

我还销毁了“rails generate controller static_pages...”,不确定是否有影响,但现在一切正常。


1
默认情况下,您将获得以下内容:
Rails.application.routes.draw do
  get 'static_pages/...'

  get 'static_pages/home'

  get 'static_pages/help'

仅删除此内容:

  get 'static_pages/...'

并且你会在 routes.rb 文件中看到这个

Rails.application.routes.draw do

  get 'static_pages/home'

  get 'static_pages/help'

0

在routes.rb文件中更改您的默认根目录。

get "static_pages#home"  TO root to: 'static_pages#home'

我也遇到了同样的问题,上述更改对我有效。


0
在我的情况下,这是由于一些路由中的后缀斜杠'/'引起的。例如:
post 'load_query/'

去掉这个后缀后

post 'load_query'

它运行成功了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接