2014-08-30 62 views
23

我得到耙路線錯誤「缺少:航線上定義操作鍵」

$ 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................ 

這裏是我的routes.rb

Rails.application.routes.draw do 
    get 'script/index' 

    get 'landing/index' 

    root 'landing/index' 
end 

什麼原因造成的問題,如何解決它。

回答

36
Rails.application.routes.draw do 
    get 'script/index' => 'script#index' 
    get 'landing/index' => 'landing#index' 
    root 'script#index' 
end 
+5

我不知道爲什麼這個工程 – 2016-04-08 10:31:27

+0

基本上你定義都如何根會看(左),以及導軌如何實際找到它(右)。 – tfantina 2016-11-04 22:20:47

+0

重要提示:在**右側**使用**#**符號。 (這是我的錯誤原因。) – Beauty 2017-05-28 14:25:00

0

我有同樣的錯誤運行rails g

如果您運行使用routes.rb的命令,則該文件必須無錯才能使命令正常工作。

在你的情況,你有路徑,但你沒有匹配他們的行動,所以routes.rb文件被打破。你需要像get 'landing/index' => 'my_controller#my_action'

0

萬花筒的代碼工作得很好。下面是一個稍微簡潔的版本。

Rails.application.routes.draw do 
    get 'script/index' 
    get 'landing/index' 
    root 'script#index' 
end 

滑軌按照慣例與#替換/添加箭頭(=>)的左側。

6

你可以做到這一點的方法很多,這些所有的工作:

  • 得到 '腳本/指數'
  • 得到 '腳本/指數'=> '腳本#指數'
  • 得到「腳本/指數」,到: '劇本#指數'

想想路徑第一和控制器#方法的追隨。

根是一個特殊的情況下,始終以:根「腳本#指數」

2

變化 root 'landing/index'root 'landing#index'

+0

9年在軌道編程,不知何故我錯過了這個哈哈,謝謝 – Alexis 2017-11-07 12:28:54