2015-04-19 81 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