Note that there are some explanatory texts on larger screens.

plurals
  1. PORails paperclip URL upload problem & How to make it DRY
    primarykey
    data
    text
    <p>I am trying to create URL upload with paperclip. </p> <p>I have followed this guide: <a href="http://trevorturk.com/2008/12/11/easy-upload-via-url-with-paperclip/" rel="nofollow">http://trevorturk.com/2008/12/11/easy-upload-via-url-with-paperclip/</a></p> <p>The problem is that nothing gets uploaded when I use the image_url fields. I know my code isnt very dry therefor it would be nice if someone had, some tips to rewrite the code.</p> <p>I have 2 attached images and therefor 2 image URLs.</p> <p>My konkurrancers table: </p> <pre><code>photo_file_name varchar(255) photo_content_type varchar(255) photo_file_size int(11) photo_updated_at datetime photo2_file_name varchar(255) photo2_content_type varchar(255) photo2_file_size int(11) photo2_updated_at datetime image_remote_url varchar(255) image_remote_url_2 varchar(255) </code></pre> <p>My konkurrancer model:</p> <pre><code>class Konkurrancer &lt; ActiveRecord::Base has_attached_file :photo, :url =&gt; "/public/images/billeder/photo/:id/:basename.:extension", :path =&gt; ":rails_root/public/images/billeder/photo/:id/:basename.:extension" has_attached_file :photo2, :url =&gt; "/public/images/billeder/photo2/:id/:basename.:extension", :path =&gt; ":rails_root/public/images/billeder/photo2/:id/:basename.:extension" before_validation :download_remote_image, :if =&gt; :image_url_provided? before_validation :download_remote_image_2, :if =&gt; :image_url_2_provided? validates_presence_of :image_remote_url, :if =&gt; :image_url_provided?, :message =&gt; 'is invalid or inaccessible' validates_presence_of :image_remote_url_2, :if =&gt; :image_url_2_provided?, :message =&gt; 'is invalid or inaccessible' private def image_url_provided? !self.image_url.blank? end def image_url_2_provided? !self.image_url_2.blank? end def download_remote_image self.photo = do_download_remote_image self.image_remote_url = image_url end def download_remote_image_2 self.photo2 = do_download_remote_image_2 self.image_remote_url_2 = image_url_2 end def do_download_remote_image io = open(URI.parse(image_url)) def io.original_filename; base_uri.path.split('/').last; end io.original_filename.blank? ? nil : io rescue # catch url errors with validations instead of exceptions (Errno::ENOENT, OpenURI::HTTPError, etc...) end def do_download_remote_image_2 io = open(URI.parse(image_url_2)) def io.original_filename; base_uri.path.split('/').last; end io.original_filename.blank? ? nil : io rescue # catch url errors with validations instead of exceptions (Errno::ENOENT, OpenURI::HTTPError, etc...) end end </code></pre> <p>My controller create action:</p> <pre><code> def create @konkurrancer = Konkurrancer.new(params[:konkurrancer]) respond_to do |format| if @konkurrancer.save format.html { redirect_to(:admin_konkurrancers, :notice =&gt; 'Konkurrancer was successfully created.') } format.xml { render :xml =&gt; :admin_konkurrancers, :status =&gt; :created, :location =&gt; @konkurrancer } else format.html { render :action =&gt; "new" } format.xml { render :xml =&gt; @konkurrancer.errors, :status =&gt; :unprocessable_entity } end end end </code></pre> <p>My form: </p> <pre><code>&lt;%= simple_form_for [:admin, @konkurrancer], :html =&gt; { :multipart =&gt; true } do |f| %&gt; &lt;%= f.label :upload_125x125 %&gt; &lt;%= f.file_field :photo, :label =&gt; '125x125', :style =&gt; 'width:250;' %&gt; &lt;%= f.input :image_url_2, :label =&gt; 'URL 125x125', :style =&gt; 'width:250;' %&gt; &lt;%= f.label :upload_460x60 %&gt; &lt;%= f.file_field :photo2, :label =&gt; '460x58', :style =&gt; 'width:250;' %&gt; &lt;%= f.button :submit, :value =&gt; 'Create konkurrence' %&gt; &lt;% end %&gt; </code></pre>
    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