繁体   English   中英

使用Ajax删除嵌套评论

[英]Delete nested comments with Ajax

我在删除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或类似的东西。 这里的任何建议将不胜感激。 谢谢。

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

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

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

实际上,注释已删除,因为日志显示查询:

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!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM