2012-12-27 115 views
1

的Rails:路由錯誤

軌產生支架控制器運動員的名字:字符串
軌產生示範單位...... sportler_id:整數

/app/controllers/sportlers_controller.rb
..
DEF add_einheit
運動員= Sportler.find(PARAMS [:ID])
@einheit = Einheit.new(:運動員=>運動員)
渲染:模板=> 「統一/編輯」

..

/app/views/sportles/index.html.erb - 當我切下面的路線錯誤
此源代碼doesnet出現了

...

<td><br> 
    <%= link_to "Trainingseinheit hinzufügen", :action => "add_einheit", :id => sportler %><br> 
</td><br> 

..

的routes.rb

FITAPP2 :: Application.routes.draw做
資源:型男

路由錯誤

沒有路由匹配{:動作=> 「add_einheit」:ID = groesse># 「3」,created_at: 「2012年12月27日15時56分04秒」,的updated_at: 「2012年12月27日15時56分04秒」>
:控制面板=>「運動員」}
嘗試運行耙路線可用路由的更多信息。

+1

Jordie,我只是重新格式化你的整個文章,但隨後刪除的變化。 [瞭解StackOverflow風格的降價](http://stackoverflow.com/editing-help) –

回答

1

由於路由錯誤建議你應該「嘗試運行耙路線可用路線的更多信息。」 問題的關鍵是,你顯然沒有爲add_einheit方法

+0

我該如何定義這樣的規則? – ubuseral

0
resources :sportlers 

此代碼只會創建有關CRUD操作路線(新建,編輯,更新VSS ...) 要使用 「add_einheit」 行動編輯routes.rb中;

resources :sportlers do 
    member do 
     get "add_einheit" 
    end 
end 

如果發送就像一個參數「ID」使用 「成員做」 使用別的 「收藏做」在你的routes.rb文件 。

+0

是的,我插入你的代碼後出現的錯誤確實不錯。 – ubuseral

+0

我做到這一點後,錯誤doenst發生,但當我點擊鏈接視圖不會出現 – ubuseral

+0

這是因爲你的模板是不是在這個行動的正確位置。將模板移動到'sportlers/add_einheit'或強制它使用'render:action =>:edit,:controller =>:einheits'來渲染你的模板。 – PinnyM

0

你確定你有型男下查看文件夾一個html文件中指定的路由規則, 像視圖/型男/ add_einheit.html.erb?

+0

不,但_form.html.erb,edit.html.erb,new.html.erb,show.html.erb – ubuseral

+0

add_einheit是我的控制器中的一個方法 – ubuseral

+0

您的html文件必須與您的方法具有相同的名稱,if你不要在你的方法塊中指定一個特定的html文件。所以如果你在sportlers控制器中有一個add_einheit方法,並且你想從這個方法渲染一個html文件,你可以設置/sportlers/add_einheit.html.erb文件。 – kader

0

我把班長的解決方案。 它的功能,但現在我是一個路由錯誤。行動=>運動員/ update_einheit犯規功能。我不明白的普林西普。

FITAPP2 :: Application.routes.draw做 資源:sportlers做 成員做 得到 「add_einheit」 GET 「update_einheit」 結束 結束 結束

我嘗試上述延伸路線與第二個條目獲取update_einheit - 它可以實現功能 路由錯誤

沒有路由匹配{:action =>「update_einheit」,:id =>#,:controller =>「sportlers」} 嘗試運行耙路線以獲取有關可用路線的更多信息。

耙路線:

add_einheit_sportler GET /sportlers/:id/add_einheit(.:format)sportlers

add_einheit

update_einheit_sportler GET /sportlers/:id/update_einheit(.:format)sportlers

update_einheit

  sportlers GET /sportlers(.:format)     sportlers 

指數

    POST /sportlers(.:format)     sportlers 

創建

 new_sportler GET /sportlers/new(.:format)    sportlers 

 edit_sportler GET /sportlers/:id/edit(.:format)   sportlers 

編輯

  sportler GET /sportlers/:id(.:format)    sportlers 

顯示

    PUT /sportlers/:id(.:format)    sportlers 

更新

    DELETE /sportlers/:id(.:format)    sportlers 

破壞

+0

如果您採用了該解決方案,請點擊接受答案左側的箭頭將其標記爲「已接受」。 – PinnyM