0

我一直在尝试删除作为可评论宝石创建的评论。

我的代码:

<%= link_to "×", comment_delete_place_path(comment.id), :method => :delete, :remote => true, :confirm => "Are you sure you want to remove this comment?", :disable_with => "×", :class => 'close' %>

控制器 :

def comment_destroy
    if params[:comment]
      @comment = Comment.find(params[:id])
      if @comment.destroy
        respond_to do |format|
          format.html { redirect_to @place, notice: 'The comment was successfully deleted!'}
          format.js
      end
    end
  end
end

路线:

delete 'places/comment/:id' => 'places#comment_destroy', as: 'comment_delete_place'

它没有给我任何错误,但它不会删除评论。我哪里错了?

4

2 回答 2

1

使用 GET 以外的任何方法的 link_to 实际上是一个坏主意,因为可以右键单击链接并在新选项卡/窗口中打开,并且因为这只是复制 url(而不是方法),所以它会因非获取链接而中断. 此外,网页索引蜘蛛点击链接,即使有问题的链接可能只对登录用户可用(因此不是蜘蛛),或者有一个“确认”,它仍然是不好的做法。

最好使用 button_to 代替,这会使 rails 生成一个表单来产生相同的结果。

从实用的角度来看,按钮更好(出于上述原因),但从概念的角度来看,它们也更好:一般来说,链接应该“带你去某个地方”,而按钮应该“做某事”。最好将这两个基本功能分开。

尝试 button_to(您需要检查 api 中的语法)。如果它仍然不起作用,请检查表单中的 url,并将其与来自的输出进行比较rake routes | grep comment

于 2014-05-07T13:52:31.900 回答
1

在你的远程请求中,没有comment参数(只id提供了参数),所以下面一行总是false,你可以去掉它:

 if params[:comment]
于 2014-05-07T13:55:55.950 回答