Ruby on rails 删除评论链接不工作(作为可评论)

Ruby on rails 删除评论链接不工作(作为可评论),ruby-on-rails,Ruby On Rails,在我的Rails应用程序中,每个帖子都有一个评论部分,用户可以在其中留下评论。我希望每个评论都有一个删除链接,但我就是不能让它工作。我在这里使用的是acts_作为可评论的宝石 posts/show.html.erb <% @comments.each do |comment| %> <p><strong><%= comment.user.username %></strong></p> <p class="co

在我的Rails应用程序中,每个帖子都有一个评论部分,用户可以在其中留下评论。我希望每个评论都有一个删除链接,但我就是不能让它工作。我在这里使用的是acts_作为可评论的宝石

posts/show.html.erb

<% @comments.each do |comment| %>
  <p><strong><%= comment.user.username %></strong></p>
  <p class="comment"><%= comment.comment %><p>
  <%= link_to "Delete", [@post, comment], method: :delete %> 
<% end %>

谢谢你的帮助!:)

destroy
操作中,您根本没有销毁它,您需要添加
@comment.destroy
从数据库中删除注释

def destroy
  @comment = Comment.find(params[:id]
  @comment.destroy # add this line
  redirect_to :back
end 

希望有帮助

更改销毁操作,如下所示:

def destroy
  @post = Post.find(params[:post_id])
  @comment = @post.comments.find(params[:id])
  @comment.destroy
  redirect_to :back
end

通过这种方式,您可以确保删除与特定帖子相关的评论。这是rails提供的一种查看方式。

你能告诉我,当你试图删除评论时,会出现什么错误吗?嘿,我一点错误都没有。单击“删除”链接时,页面将刷新,但评论不会被删除。请粘贴您的服务器日志here@Joshua您可以检查登录服务器。尝试在destroy方法中写入exit,这样您就可以在那里跟踪代码了。@Joshua这就是您在控制器中编写的代码。单击后重定向到“返回”。您已写入以删除注释?或在单个查询中,
comment=comment.find_by(id:params[:id],post_id:params[:post_id])
def destroy
  @comment = Comment.find(params[:id]
  @comment.destroy # add this line
  redirect_to :back
end 
def destroy
  @post = Post.find(params[:post_id])
  @comment = @post.comments.find(params[:id])
  @comment.destroy
  redirect_to :back
end