gpt4 book ai didi

javascript - 使用 Ajax 删除嵌套注释

转载 作者:行者123 更新时间:2023-12-02 17:00:16 25 4
gpt4 key购买 nike

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

销毁.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>

评论 Controller

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" %>

坑.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

评论.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!

关于javascript - 使用 Ajax 删除嵌套注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25793808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com