2011-05-30 38 views
2

我正在尝试使用回形针创建URL上传。Rails回形针URL上传问题及如何使它干DRY

我按照这个指南:http://trevorturk.com/2008/12/11/easy-upload-via-url-with-paperclip/

的问题是,当我使用IMAGE_URL领域没有被上传。我知道我的代码不是很干燥,如果有人有一些提示重写代码,那将会很好。

我有2个附加图像,因此有2个图像网址。

我konkurrancers表:

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) 

我konkurrancer型号:

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 

我控制器创建行动:

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 

我的形式:

<%= 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 %> 

回答

0

要解决一个重复模式的问题,你会想,而不是为你的照片单独的一类,其存储外键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 

另外,我觉得你想从URL下载远程图像,然后将其保存到Paperclip中。采用开放式URI(我相信你已经是),你可以做到这一点,像这样:

# 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 

这将节省您的网址为临时文件,然后可以传递到回形针定期处理。

+0

我应该如何验证图像? – Jon 2011-05-30 22:03:59

+0

回形针的所有验证都会将它们验证为正常,所以只要您下载并使用before_validation回调(如您现在)分配图像,就可以使用这些验证。 – Felix 2011-05-30 22:06:15

+0

我是否需要open-uri – Jon 2011-05-30 22:12:13

3

在回形针的最新版本中(pull请求已合并但我不确定发布)paperclip> 3.1.3(也许3.2即将推出;也许3.1.4),这变得更加容易。

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

上面应该注意下载/ tempfile文件/文件名和filecontent类型。

享受! :)