Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing carrierwave to upload one image to multiple storage location
    primarykey
    data
    text
    <p>I would like to be able to upload one image into two different locations: one location would be on the local filesystem (of the server) and the other would be Amazon S3 (the Amazon S3 location would be optional).</p> <p>My current environment is Rails 3.2.8, Ruby 1.9.3, with Carrierwave used for uploading the file. </p> <p>I've had some success using the following method:</p> <p>Model</p> <pre><code>class Image &lt; ActiveRecord:Base attt_accessor :remote before_save :configure_for_remote mount_uploader :image, ImageUploader #stores images locally mount_uploader :image_remote, ImageRemoteUploader #store images on S3 def configure_for_remote if self.remote=="1" self.image_remote = self.image.dup end end end </code></pre> <p>Relevant view form fields (simple form syntax)</p> <pre><code>&lt;p&gt;&lt;%= f.input :image, as: :file %&gt;&lt;/p&gt; &lt;p&gt;&lt;%= f.input :remote, as: :boolean %&gt;&lt;/p&gt; </code></pre> <p>The user checks the "remote" checkbox in the form and chooses the image to upload. The before_save callback stores a duplicate of image into image_remote, the file is processed by their respective uploaders, and I have my desired result.</p> <p>However, I'm starting to run into problems when I want to update that field. For example, if the user chooses to first upload the file locally and not to S3 (does not check the remote checkbox), then later comes back to the form and checks the remote checkbox. In this case, the before_save callback does not get run because no real active record column has been changed (only the remote flag). I've tried to use before_validation, but this fails to work (the image_remote uploader stores the proper filename in the image_remote column, but the image does not get uploaded to S3). Obviously something is changing between the before_validation and the before_save (image attribute is being converted to and uploader?) but I can't seem to figure out why this doesn't work.</p> <p>With all this being said, I think my approach with using <code>dup</code> is a bit of a hack, and I'm hoping someone can advise me in a more elegant way of reaching my goal.</p> <p>Thanks for your help. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload