In my rails app, there is a javascript callback that runs after a post is created. This callback polls a processing endpoint to check if an image has finished processing and then updates the view accordingly:
create.js.erb create callback
// initiating the poll for processed image
function imageProcessed() {
$.ajax({ url: "/posts/" + <%= @post.id %> + "/processing"})
.done(function(data) {
if (data.processing) {
setTimeout(imageProcessed(), 2000);
} else {
// displaying the processed image url
$("#js-capture-container").html('<img src="' + data.image_url + '" alt="image">');
}
});
}
imageProcessed();
posts_controller.rb endpoint
def processing
data = { processing: @post.image_processing }
if <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="46670636293532682f2b272123">[email protected]</a>_processing?
data[:image_url] = @post.image_url(:pair)
end
render json: data
The current setup works well, but I am interested in taking the data received in the ajax call and rendering an existing _post.html.erb partial with this data.
I am looking for a way to return an updated instance of @post and then render that object into the partial.
"<%= escape_javascript(render @post) %>"
in create.js.erb
Any suggestions or ideas would be greatly appreciated. Thank you!