У меня проблема с удалением комментариев с помощью AJAX.Когда я удаляю комментарий, ничего не происходит на странице до обновления, но данные удаляются в базе данных.Я искал эту тему более 2 часов и все еще не смог найти anwser.
Просмотры:
#annoucements/show.html.haml
=render @comments
#comments/_comment.html.haml
%ul
%p
%b User:
= comment.user.username
= time_ago_in_words(comment.created_at)
%p
%b Content:
= comment.content
- if comment.user.id == current_user[:id]
= link_to 'Delete comment', annoucement_comment_path(@annoucement, comment), method: :delete, data: {confirm: 'Are you sure?'}, remote: true
Контроллер:
#comment_controller.rb
def destroy
@annoucement = Annoucement.find(params[:annoucement_id])
@comment = Comment.find(params[:id])
@comment.destroy
respond_to do |format|
format.html { redirect_to @annoucement }
format.js
end
end
JS:
#destroy.js.erb
$('#comment_<%= @comment.id %>').remove();
Консоль:
Started DELETE "/annoucements/9/comments/142" for 127.0.0.1 at 2018-04-26 01:11:53 +0200
Processing by CommentsController#destroy as JS
Parameters: {"annoucement_id"=>"9", "id"=>"142"}
Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = ? LIMIT ? [["id", 142], ["LIMIT", 1]]
Annoucement Load (0.1ms) SELECT "annoucements".* FROM "annoucements" WHERE "annoucements"."id" = ? LIMIT ? [["id", 9], ["LIMIT", 1]]
CACHE Comment Load (0.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = ? LIMIT ? [["id", 142], ["LIMIT", 1]]
(0.1ms) begin transaction
SQL (0.3ms) DELETE FROM "comments" WHERE "comments"."id" = ? [["id", 142]]
(82.4ms) commit transaction
Rendering comments/destroy.js.erb
Rendered comments/destroy.js.erb (0.9ms)
Completed 200 OK in 95ms (Views: 5.7ms | ActiveRecord: 82.9ms | Solr: 0.0ms)