Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your solution of using <code>File.open</code> would work, but you should verify that the name is valid with <code>File.basename</code> so someone couldn't pass <code>../../secret_credentials.yml</code> and expose info you don't want them to. Also checking it against the list of preset images would be a good idea.</p> <p>However, you can do this more efficiently by skipping CarrierWave in this case.</p> <p>Add a new field called <code>library_image</code>, when someone wants to use a preset image, then you set <code>library_image</code>, if they want to use their own photo, unset <code>library_image</code> and upload the photo as normal. Then add a helper method like:</p> <pre><code>def avatar_url if self.library_image? "http://example.com/images/#{self.library_image}" else self.picture.url end end </code></pre> <p>This assumes that you can find the preset images at <code>http://example.com/images/[name]</code> and you are using <code>mount_uploader</code> in CarrierWave named <code>picture</code>.</p> <p>Now anytime you want to display their picture, you would call <code>avatar_url</code> which will check if it should return the preset URL, or the picture they uploaded.</p> <p>An example of how you would update <code>library_image</code> or <code>picture</code> based on your original question:</p> <pre><code>if params[:file].is_a?(Hash) model.library_image = params[:file][:url] model.remove_picture = true else model.library_image = nil model.picture = params[:file] end </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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