0
在嵌套的路線評論我在我的應用程序的情況是,comments
嵌套在blog
裏面是這樣的:刪除的link_to對單獨的頁面
resources :blogs do
resources :comments
end
我有意見的users#show
頁上的列表中用戶需要能夠刪除評論。我users_controller
是這樣的:
def show
@user = User.find(params[:id])
@comments = Comment.where(approved: false)
end
我comments_controller
刪除方法是這樣的:
def destroy
@blog = Blog.find(params[:blog_id])
@comment = blog.comments.find(params[:id])
@comment.destroy
redirect_to blog_path(@blog), notice: 'Comment has been deleted.'
end
最後,這裏是我的users#show
頁面上刪除鏈接:
<% @comments.each do |comment| %>
<p><strong><%= comment.body %></strong><br />
<em><%= comment.user.first_name %> <%= comment.user.last_name %></em></p>
<%= link_to blog_comment_path(@blog, comment), method: :delete, data: { confirm: 'Are you sure?' } do %>
<i class="icon ion-trash-a"></i>
<% end %>
<%= link_to approve_comment_path(comment), method: :post do %>
<i class="icon ion-checkmark"></i>
<% end %>
<% end %>
我出現以下錯誤:
No route matches {:action=>"show", :blog_id=>nil, :controller=>"comments", :id=>"1"} missing required keys: [:blog_id]
任何人都可以看到我要去哪裏錯了嗎?
愚蠢的錯誤。優秀。謝謝! – Liz
我很高興聽到這個消息。另一個訣竅:您可以使用'[comment.blog,comment]'而不是'blog_comment_path(comment.blog,comment)'' – fongfan999