以下循环显示与特定项目关联的每个评论,并允许评论所有者(用户)编辑或删除评论。这样就可以了。
现在,我希望管理员能够删除评论。唯一的问题是,当我单击“删除...”时,与特定项目相关的所有注释都会被删除,而不仅仅是该注释。
有什么解决方法吗?
<% @comments.each do |comment| %>
<% if current_user == comment.user %>
<%= link_to "Delete", destroy_item_comment_path(comment.item_id, comment.id),
method: :delete, data: { confirm: I18n.t("comment_delete_alert")}, :class=>"glyphicon glyphicon-trash" %>
<% elsif current_admin %>
<%= link_to "Delete", admin_destroy_item_comment_path(comment.item_id, comment.id),
method: :delete, data: { confirm: I18n.t("comment_delete_alert")}, :class=>"glyphicon glyphicon-trash" %>
<% end %>
<% end %>
控制器方法:
before_action :find_comment, only: [:destroy]
def destroy
@comment.destroy
redirect_back(fallback_location: root_path)
flash[:notice] = "Comment has been deleted"
end
def find_comment
@comment = @item.comments.find(params[:id])
end
更新1
以管理员身份删除评论时服务器记录
Comment Load (0.9ms) SELECT "comments".* FROM "comments" WHERE "comments"."user_id" = $1 [["user_id", 213]]
(0.2ms) BEGIN
SQL (0.4ms) DELETE FROM "comments" WHERE "comments"."id" = $1 [["id", 14]]
SQL (0.4ms) DELETE FROM "comments" WHERE "comments"."id" = $1 [["id", 15]]
SQL (0.3ms) DELETE FROM "comments" WHERE "comments"."id" = $1 [["id", 16]]
(1.2ms) COMMIT
Completed 302 Found in 14123ms (ActiveRecord: 36.3ms)
答案 0 :(得分:0)
我的直觉是您的路由配置不正确。您可以点击
rails routes
检查admin_delete_comment_path配置。
尝试更改
admin_destroy_item_comment_path(comment.item_id,comment.id)
收件人:
destroy_item_comment_path(commend.item_id, comment.id)