簡體   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