Rails回形针URL上传问题以及如何使其干燥

我正在尝试使用paperclip创建URL上传。

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

问题是当我使用image_url字段时没有上传任何内容。 我知道我的代码不是很干,因为如果有人有一些提示可以改写代码。

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

我的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  "/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 

我的表格:

  { :multipart => true } do |f| %>   '125x125', :style => 'width:250;' %>  'URL 125x125', :style => 'width:250;' %>   '460x58', :style => 'width:250;' %>  'Create konkurrence' %>  

在最新版本的paperclip(拉请求已合并,但我不确定发布)paperclip> 3.1.3(可能3.2即将发布;也许3.1.4)这变得更加容易。

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

以上应该注意download / tempfile stuff / filename和filecontent类型。

请享用! 🙂

要解决重复模型的问题,您需要为照片创建一个单独的类,以便将外键存储到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中。 使用open-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 

这会将您的URL保存为临时文件,然后您可以将其传递给Paperclip进行常规处理。