使用Ajax删除嵌套的注释

Delete nested comments with Ajax

本文关键字:注释 嵌套 删除 Ajax 使用      更新时间:2023-09-26

我在使用Ajax删除注释时遇到一些问题。我想我很接近,但不确定,我想听听建议。还在习惯jquery之类的。我可以通过ajax删除注释,但实际上不能删除记录本身,所以这可能是一个简单的语法问题。

destroy.js.erb

$('#remove_comment').remove();

我想我需要用评论ID来标记它,但我遇到了问题,因为评论嵌套在Pit模型下。

_comment.html.erb

<div class = "well", id = "remove_comment">
    <p>
      <%= comment.body %>
    <p>posted by: <%= comment.user.name %></p>
    <div class = "response">
       <p class = "like-response">Was this response persuading to you?</p>
       <%= link_to "Yes", pit_comment_like_path(@pit, comment), method: :put %>
       <%= link_to "No", pit_comment_dislike_path(@pit, comment), method: :put %>
    </div>
    <div class = "response-convince">
      <p class = "dislike-comment">
        <%= comment.get_dislikes.size %> users found this response unpersuasive
      </p>
       <p class = "like-comment">
        <%= comment.get_likes.size %> users found this response persuasive</p>
      </p>
    </div>
    <p>

    <%if comment.user == current_user %>
     <%= link_to 'Destroy Comment', [@pit, comment],
                 method: :delete,
                 data: { confirm: 'Are you sure?' }, remote: true, class: "btn btn-default" %>
    </p>
  <% end %>  
</div>

注释控制器

def destroy
  @pit = Pit.find(params[:pit_id])
  @comment = @pit.comments.find(params[:id])
  @comment.destroy
    respond_to do |format|
        format.html {redirect_to pit_path(@pit)}
        format.js {}
    end

日志似乎工作正常

Started DELETE "/pits/398/comments/63" for 127.0.0.1 at 2014-09-11 12:31:08 -0500
Processing by CommentsController#destroy as JS
  Parameters: {"pit_id"=>"398", "id"=>"63"}
  Pit Load (0.1ms)  SELECT  "pits".* FROM "pits"  WHERE "pits"."id" = ? LIMIT 1  [["id", 398]]
  Comment Load (0.1ms)  SELECT  "comments".* FROM "comments"  WHERE "comments"."pit_id" = ? AND "comments"."id" = ? LIMIT 1  [["pit_id", 398], ["id", 63]]
   (0.1ms)  begin transaction
  ActsAsVotable::Vote Load (0.1ms)  SELECT "votes".* FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ?  [["votable_id", 63], ["votable_type", "Comment"]]
  SQL (0.3ms)  DELETE FROM "comments" WHERE "comments"."id" = ?  [["id", 63]]
   (3.0ms)  commit transaction
  Rendered comments/destroy.js.erb (0.5ms)
Completed 200 OK in 13ms (Views: 3.9ms | ActiveRecord: 3.7ms)

这是我在pits/show.html.erb 中的相关标记

    <h3>Responses</h3>
  <div id = "comment_body">
    <%= render @pit.comments %>
  </div>
    <%= render partial: "comments/form" %>

pit.rb

class Pit < ActiveRecord::Base
  validates :topic, :author, :summary, presence: true
  acts_as_votable
  has_many :comments
  belongs_to :user
  mount_uploader :image, ImageUploader
end

comment.rb

class Comment < ActiveRecord::Base
  acts_as_votable
  belongs_to :pit
  belongs_to :user
end

我的create.js.erb可以正确插入所有内容。我只需要删除它,我想我需要传入注释ID或类似的东西。如有任何建议,我们将不胜感激。谢谢

实际上,注释正在被删除,因为日志显示了查询:

SQL (0.3ms)  DELETE FROM "comments" WHERE "comments"."id" = ?  [["id", 63]]

我猜您的jQuery在回调后没有删除适当的注释。您可以尝试更改_comment.html.erb:的视图代码

<div class = "well", id = "remove_comment_<%= comment.id %>">
    <p>
      <%= comment.body %>

然后你的destroy.js.erb:

$("#remove_comment_<%= @comment.id %>").remove(); // Since @comment will be available in the variable here!

创建一个具有data-id属性的link_to,单击时使用jquery向控制器发出GET请求。

首先,在config/routes.rb:中为删除操作创建一个路由

get 'delete_comment' => 'comments#delete_comment'

接下来,给你的控制器添加一个方法(肯定是CommentsController):

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

现在,在您的视图中设置一个链接:

= link_to "Remove Comment", "#", :class => "remove_comment", :'data-id' => @comment.id

现在设置jquery GET请求,以便在单击链接时激发:

$(".remove_comment").click(function(event){
  event.preventDefault();
  $.get("/delete_comment", {id: $(this).attr("data-id") } );
});

在本例中,您需要将delete.js.erb文件重命名为delete_comment.js.erb