我在使用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或其他东西。这里的任何建议将不胜感激。感谢。
答案 0 :(得分:1)
使用data-id属性创建link_to,单击时使用jquery向控制器发出GET请求。
首先,在config / routes.rb中为删除操作创建一个路由:
get 'delete_comment' => 'comments#delete_comment'
接下来,向控制器添加一个方法(可以说是CommentController):
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
答案 1 :(得分:1)
实际上,当日志显示查询时,评论正在被删除:
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!