In my rails app I have a javascript callback after the a post is created that polls a processing endpoint to check if an image has finished processing and then updates the view like this:
create.js.erb create callback
// start polling for processed image
function imageProcessed() {
$.ajax({ url: "/posts/" + <%= @post.id %> + "/processing"})
.done(function(data) {
if (data.processing) {
setTimeout(imageProcessed(), 2000);
} else {
// append image url when processed
$("#js-capture-container").html('<img src="' + data.image_url + '" alt="image">');
}
});
}
imageProcessed();
posts_controller.rb endpoint
def processing
data = { processing: @post.image_processing }
if !@post.image_processing?
data[:image_url] = @post.image_url(:pair)
end
render json: data
end
This works fine, but I would like to take the data returned in the ajax and render the already existing _post.html.erb partial with this data.
Something like return an updated instance of @post and then render that object into the partial.
"<%= escape_javascript(render @post) %>"
in create.js.erb
Any ideas would greatly help, thanks!