我正在尝试使用回形针创建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 %>
我应该如何验证图像? – Jon 2011-05-30 22:03:59
回形针的所有验证都会将它们验证为正常,所以只要您下载并使用before_validation回调(如您现在)分配图像,就可以使用这些验证。 – Felix 2011-05-30 22:06:15
我是否需要open-uri – Jon 2011-05-30 22:12:13