开发者

Rails paperclip URL upload problem & How to make it DRY

开发者 https://www.devze.com 2023-03-09 14:37 出处:网络
I am trying to create URL upload with paperclip. I have followed this guide: http://trevorturk.com/2008/12/11/easy-upload-via-url-with-paperclip/

I am trying to create URL upload with paperclip.

I have followed this guide: http://trevorturk.com/2008/12/11/easy-upload-via-url-with-paperclip/

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.

I have 2 attached images and therefor 2 image URLs.

My konkurrancers table:

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)

My konkurrancer model:

class Konkurrancer < ActiveRecord::Base
has_attached_file :photo,
                  :url  => "/public/images/billeder/photo/:id/:basename.:extension",
                  :path => ":rails_root/public/images/billeder/photo/开发者_开发百科:id/:basename.:extension"
has_attached_file :photo2,
                  :url  => "/public/images/billeder/photo2/:id/:basename.:extension",
                  :path => ":rails_root/public/images/billeder/photo2/:id/:basename.:extension"

 before_validation :download_remote_image, :if => :image_url_provided?
  before_validation :download_remote_image_2, :if => :image_url_2_provided?
  validates_presence_of :image_remote_url, :if => :image_url_provided?, :message => 'is invalid or inaccessible'
  validates_presence_of :image_remote_url_2, :if => :image_url_2_provided?, :message => '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

My controller create action:

  def create
    @konkurrancer = Konkurrancer.new(params[:konkurrancer])

    respond_to do |format|
      if @konkurrancer.save
        format.html { redirect_to(:admin_konkurrancers, :notice => 'Konkurrancer was successfully created.') }
        format.xml  { render :xml => :admin_konkurrancers, :status => :created, :location => @konkurrancer }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @konkurrancer.errors, :status => :unprocessable_entity }
      end
    end
  end

My form:

<%= simple_form_for [:admin, @konkurrancer], :html => { :multipart => true } do |f| %>
    <%= f.label :upload_125x125 %>
    <%= f.file_field :photo, :label => '125x125', :style => 'width:250;' %>
    <%= f.input :image_url_2, :label => 'URL 125x125', :style => 'width:250;' %>
    <%= f.label :upload_460x60 %>
    <%= f.file_field :photo2, :label => '460x58', :style => 'width:250;' %>
    <%= f.button :submit, :value => 'Create konkurrence' %>
<% end %>


In the latest version of paperclip (pull request has been merged but i'm not sure about the release) paperclip > 3.1.3 (maybe 3.2 is upcoming; maybe 3.1.4) this is become even easier.

self.photo = URI.parse("http://something.com/blah/image.png")

The above should take care of download/tempfile stuff/filename and filecontent type.

Enjoy! :)


To fix the problem of an repetitive model, you'll want to instead create a separate class for your photos that stores a foreign key to the konkurrence:

class Photo < ActiveRecord::Base
    has_attached_file ...
    belongs_to :konkurrence
    ...
end

class Konkurrence < ActiveRecord::Base
    has_many :photos, :dependent => :destroy
    accepts_nested_attributes_for :photos, :allow_destroy => true
    ...
end

Also, I think you're trying to download a remote image from a URL and then save this into Paperclip. Using open-uri (as I believe you already are), you can do this like so:

# open a tempfile using the last 14 chars of the filename
t = Tempfile.new(image_url.parameterize.slice(-14, 14))
t.write(open(image_url).read)
t.flush
t # return the File. You can then set the paperclip attribute to this File, eg, self.photo = t

This will save your URL as a temporary file which you can then pass on to Paperclip for regular processing.

0

精彩评论

暂无评论...
验证码 换一张
取 消