-1

I'm writing an app in which user will be allowed to put filters on image (RMagick). New image is saved and uploaded with carriervawe but it is not available until page gets reloaded. Do you know how to upload and display image without reloading or redirecting?

Tezert
  • 1

1 Answers1

0

You've got a few options here to solve this one.

I would imagine a good way to handle the problem would be:

  • User Uploads the Image
  • Image Processing is handled in the background

I find https://github.com/lardawge/carrierwave_backgrounder is a pretty handy gem for things like this, it'll handle the callbacks and update your database when the image has finished being processed.

With this information you could check if the image is processed, if not display a holder image and poll the database with some javascript to replace the holder image with the processed image when completed.

# view.html.erb
<% if @image.image_proccessed %>
  ## Display image
<% else %>
  ## Display holder image
  <script>
  function check_if_loaded() {
    $.post("<%=j image_processing_url(@image) %>", function(result) {
          if(result == false) {
            setTimeout(check_if_loaded, 3000); //Try again in 3 seconds
          }
          else{ // processing is finished, safe to display image
            ## Replace holder image with display Image
            );
          }
      });  
    }
  $(check_if_loaded);  
  </script>
<% end %>
Djwokian
  • 249
  • 2
  • 8