2015-04-19 80 views
0

我有這樣的組織資源:無法刪除屬於另一個資源的資源

resources :tickets do 
    resources :comments 
end 

當我嘗試使用這樣的鏈接從一個票(其中,他們都列出)刪除評論:

<table class="table"> 
     <% @ticket.comments.each do |c| %> 
      </tr> 
       <td><%= c.text %> | <%= link_to "Delete", ticket_comment_path(c), method: :delete, data: {confirm: "Are you sure?"} %></td> 
      </tr> 
     <% end %> 
    </table> 

我有一個錯誤: 無路由匹配{:行動=> 「節目」,:控制器=> 「評論」,:ID => 「5」}缺少必需的鍵:[:TICKET_ID]

我想,f或者ticket_comment_path(c)id應該是評論的ID並且應填寫ticket_id。

但不知何故,我的:id爲一票ID和:TICKET_ID是空的...

+2

嘗試'ticket_comment_path(@ticket,C)' – kddeisz

回答

1

當您使用嵌套的資源,網址看起來會像這樣

/tickets/:ticket_id/comments/:id 

因此,刪除你需要傳遞一個評論2參數均爲ticker_idcomment_id。您刪除link_to應該是這樣的

<%= link_to "Delete", ticket_comment_path(@ticket.id, c), method: :delete, data: {confirm: "Are you sure?"} %> 

來源:http://guides.rubyonrails.org/routing.html#nested-resources

+0

謝謝你的信息! –

1

嘗試ticket_comment_path(@ticket, c) - 嵌套資源既需要ID來正確地路由。您可以通過運行rake routes | grep comment看到的路線,你會看到類似DELETE /tickets/:ticket_id/comments/:id