rails carrierwave edit/update form

carrierwave, ruby-on-rails

Solution

In your listings_controller.rb, try something like:

def edit
    @listing = Listing.find(params[:id])
    @listing.image.cache!
end

Haven't tested this out myself, but I think it might work, given that image_cache field is used to circumvent this problem normally.

Problem

In my rails project I use Carrierwave to upload images to S3 via fog. So far I have the Create Read and Delete portions of the CRUD spectrum working. My problem is the edit/update portion. Im use the same `_form.html.erb` to edit that I used for creating records. When I click the edit link the form loads all of my data into the form fields for editing with the exception of the image. The form field is blank as though there is no image associated with the record. How do I update an image that is already saved to S3? Models/listing.rb ``` class Listing < ActiveRecord::Base attr_accessible :body, :price, :title, :image mount_uploader :image, ListingUploader end ``` Controllers/listings_controller.rb (edit/update portion) ``` def edit @listing = Listing.find(params[:id]) end def update @listing = Listing.find(params[:id]) if @listing.update_attributes(params [:listing]) redirect_to :action => 'show', :id => @listing else render :action => 'edit' end end ``` _form.html.erb ``` <%= form_for @listing, :html => { :multipart => true } do |l| %> <p> <%= l.label :title %> <%= l.text_field :title %> </p> <p> <%= l.label :price %> <%= l.text_field :price %> </p> <p> <label>Upload a Picture</label> <%= l.file_field :image %> <%= l.hidden_field :image_cache %> </p> <div class="image-pre"> <%= image_tag(@listing.image_url(:thumb)) if @listing.image? %> </div> <p> <%= l.label :body %> <%= l.text_area :body, :class => "tinymce" %> <%= tinymce %> </p> <%= l.submit %> <% end %> ```

Original source