问题描述
||
我正在尝试使用回形针创建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 < 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,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 %>
解决方法
在最新版本的回形针中(合并了拉纸请求,但我不确定该版本)回形针> 3.1.3(也许即将发布3.2;也许是3.1.4),这变得更加容易。
self.photo = URI.parse(\"http://something.com/blah/image.png\")
上面应该注意下载/临时文件内容/文件名和文件内容类型。
请享用! :)
, 要解决重复模型的问题,您需要为照片创建一个单独的类,该类存储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进行常规处理。