Search code examples
ruby-on-railsruby-on-rails-3.1carrierwave

Carrierwave to upload image


I'm using carrierwave to upload an image, this works fine up to now.

What i want to do is show the preview of the file to be submitted on the form after having selected the image.

At the moment the code I have is showing the previous image.

<%= image_tag(@book.cover_url(:thumb)) if @book.cover_url %>

how do I show the selected image rather than the image already uploaded?


Solution

  • You don't.

    And that has nothing to do with Carrierwave but rather with the fact that the image is not yet sent to the server by your browser so no part of the Rails stack is coming into play here.

    What you need is a way to access the image inside the <input type="file"> and display it to the user.

    This is possible by using the FileAPI that is part of HTML5, but it's only supported in modern browsers (Webkit/Gecko), and the API is not completely finished yet so it's kind of a changing target.

    I would suggest you look into tools like plupload to upload the image to the server without submitting the form and then displaying that through JavaScript.