2011-03-04 140 views
8

對不起,如果這已被問到別處,但我無法弄清楚這一點。我有一個論壇,有章節,主題和回覆。我正在嘗試編輯並從節目主題視圖中刪除回覆。這是結構:Rails 3 link_to路由(編輯)嵌套資源

resources :sections do 
    resources :topics do 
    resources :replies 
    end 
end 

所以我做了一個耙路徑,看看我連接我的編輯答覆。我看到它的edit_section_topic_reply並在我的link_to中添加了_path。現在這是我無法弄清楚的。我傳遞了什麼參數?它不應該是:

<%= link_to 'Edit', edit_section_topic_reply_path(@reply, @topic, @section) %> 

我得到一個在Topics#showActionController::RoutingError當我這樣做。

No route matches {:topic_id=>#<Topic id: 2, section_id: 2, user_id: nil, subject: "subject", body: "body", created_at: "2011-03-04 08:37:37", updated_at: "2011-03-04 21:37:16">, :controller=>"replies", :action=>"edit", :section_id=>nil, :id=>#<Section id: 2, name: "Section", description: "Section Description", created_at: "2011-03-04 07:50:56", updated_at: "2011-03-04 07:50:56">} 

現在看來似乎是不及格的ID,但巢之前,我的新課題工作正常

new_section_topic_reply_path(@topic, @section) 

回答

14

我真的不喜歡的link_to助手的這一方面。在使代碼更易讀,不易出錯的利益,我建議你可以明確你正在傳遞中的ID。

<%= link_to 'Edit', edit_section_topic_reply_path(:id => @reply.id, 
                :topic_id => @topic.id, 
                :section_id => @section.id) %> 

我碰到由於太多微妙的,看似瘋狂的bug以參數link_to的順序出現故障。

+0

啊所以在做這種方式,我意識到另一種方式響應ID沒有被調用它的具體ID。真的很愚蠢的錯誤,我在一個循環,所以當然@reply沒有說它實際上是哪個ID。 <%= link_to'編輯',edit_section_topic_reply_path(reply,@ topic,@section)%>以其他方式解決它。這樣回覆.id。謝謝您的幫助! – a3uge 2011-03-05 06:17:57

+0

另外,我倒退了。它是(@section,@ topic,path)。 – a3uge 2011-03-05 06:26:51

+0

我認爲你的意思是(@section,@topic,@reply)。 – GMA 2013-09-05 07:08:23

0

我認爲正確的順序應該是:

<%= link_to 'Edit', edit_section_topic_reply_path(@section, @topic, @reply) %> 
3

爲編輯鏈接

<%= link_to [:edit,@section,@topic,@reply] %>