Ruby-on-rails Rake 路由错误“缺少:路由定义上的操作键”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25586310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rake route Error "Missing :action key on routes definition"
提问by Tithos
I am getting
我正进入(状态
$ rake routes
rake aborted!
ArgumentError: Missing :action key on routes definition, please check your routes.
/usr/local/rvm/gems/ruby-2.1.2/gems/actionpack-4.1.5/lib/action_dispatch/routing/mapper.rb:243:in `default_controller_and_action'
/usr/local/rvm/gems/ruby-2.1.2/gems/actionpack-4.1.5/lib/action_dispatch/routing/mapper.rb:117:in `normalize_options!'
/usr/local/rvm/gems/ruby-2.1.2/gems/actionpack-4.1.5/lib/action_dispatch/routing/mapper.rb:65:in `initialize'
/usr/local/rvm/gems/ruby-2.1.2/gems/actionpack-4.1.5/lib/action_dispatch/routing/mapper.rb:1487:in `new'
/usr/local/r................
Here is my Routes.rb
这是我的 Routes.rb
Rails.application.routes.draw do
get 'script/index'
get 'landing/index'
root 'landing/index'
end
What is causing the problem and how do I fix it.
是什么导致了问题,我该如何解决。
回答by Kaleidoscope
The Rails router recognizes URLs and dispatches them to a controller's action.The error is caused by missing out the mapped action.
Rails 路由器识别 URL 并将它们分派给控制器的操作。该错误是由于缺少映射的操作引起的。
Rails.application.routes.draw do
# url action
get 'script/index' => 'script#index'
get 'landing/index' => 'landing#index'
root 'script#index'
end
回答by rtfminc
You can do it many ways, these all work:
您可以通过多种方式做到这一点,这些都有效:
- get 'script/index'
- get 'script/index' => 'script#index'
- get 'script/index', to: 'script#index'
- 获取“脚本/索引”
- 获取 'script/index' => 'script#index'
- 获取'脚本/索引',到:'脚本#索引'
Think of pathfirst and controller#methodto follow.
首先考虑路径和要遵循的控制器#方法。
Root is a special case, always: root 'script#index'
root 是一个特例,总是:root 'script#index'
回答by icedTea
Change
root 'landing/index'to
root 'landing#index'
更改
root 'landing/index'为
root 'landing#index'
回答by Mirror318
I had the same error running rails g.
我有同样的错误运行rails g。
If you run a command that uses routes.rb, the file needs to be error free for the command to work.
如果您运行使用 的命令routes.rb,则该文件需要无错误才能使命令正常工作。
In your case, you had paths, but you didn't match them to actions, so the routes.rbfile was broken. You needed something like get 'landing/index' => 'my_controller#my_action'
在您的情况下,您有路径,但您没有将它们与操作匹配,因此routes.rb文件已损坏。你需要类似的东西get 'landing/index' => 'my_controller#my_action'
回答by James Parker
Kaleidoscope's code works just fine. Below is a slightly concise version.
Kaleidoscope的代码工作得很好。下面是一个稍微简洁的版本。
Rails.application.routes.draw do
get 'script/index'
get 'landing/index'
root 'script#index'
end
Rails add the left side of the arrow(=>) by convention replacing /with #.
Rails 按照惯例添加了箭头(=>)的左侧,替换/为#。

