Answer the question
In order to leave comments, you need to log in
How to make ajax execute once?
Good afternoon, ladies and gentlemen. The following incident has overtaken:
The controller method works through ajax:
def create
@comment = @commentable.comments.new(comment_params)
@comment.user = current_user
respond_to do |format|
if @comment.save
format.js do
PrivatePub.publish_to "/comments", comment: @comment.to_json,
comment_author: @comment.user.email.to_json
render nothing: true
end
else
format.json { render json: { errors: @comment.errors.full_messages } }
end
end
end
ready = ->
PrivatePub.subscribe '/comments', (data, channel) ->
comment_author = $.parseJSON(data['comment_author'])
comment_body = $.parseJSON(data['comment']).body
commentable_id = $.parseJSON(data['comment']).commentable_id
commentable_type = $.parseJSON(data['comment']).commentable_type
$( '.' + commentable_type + '-comments#' + commentable_id).append(JST["templates/comment"]({comment_author: comment_author, comment: comment_body}))
$('textarea#comment_body').val('')
$(document).ready(ready)
$(document).on('page:load', ready)
$(document).on('page:update', ready)
Answer the question
In order to leave comments, you need to log in
The problem was turbolinks, be it wrong. Deleted - everything worked as expected. Maybe someone knows how to make friends with turbolinks scripts?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question