"销毁"控制器动作似乎不止一次循环

时间:2015-03-31 17:41:38

标签: ruby-on-rails ruby loops ruby-on-rails-4 controller

我有模型"评论",belongs_to另一个模型"用户",反过来has_many评论。在我的'用户/编辑'页面,我有一个销毁评论的链接:

用户/ edit.html.erb

<% @user.comments.each do |comment| %>
  <%= link_to "destroy", destroy_comment_path(comment.id) %>
<% end %>

comments_controller

def destroy
  @comment = Comment.find(params[:id])
  @user = @comment.user
  @comment.destroy
  redirect_to edit_user_path(@user)
end

但是我收到了一个奇怪的错误。该动作似乎不止一次循环。我收到此错误消息:

ActiveRecord::RecordNotFound in CommentsController
Couldn't find Comment with id=[ID OF THE COMMENT]

突出显示此行:

@comment = Comment.find(params[:id])

在我调用方法之前,注释肯定存在,并且在我调用它之后它不再存在。如果我删除行@comment.destroy,则操作按预期运行,并且注释不会被销毁。因此,必须在该行之后再次循环操作。但我无法想象为什么会这样做。有谁看到可能出错的地方?我绝对没有任何其他行动叫做#34; destroy&#34;。

0 个答案:

没有答案