【发布时间】:2011-12-04 23:12:28
【问题描述】:
我为我的公司模型上传了一张图片,但它仅适用于 1 张图片。 现在我希望照片模型处理两个不同的图像。
我的公司模式:
class Virksomhed < ActiveRecord::Base
has_one :photo, :dependent => :destroy
end
我的照片模型:
require 'open-uri'
class Photo < ActiveRecord::Base
belongs_to :virksomhed
attr_accessor :image_url
has_attached_file :image,
:styles => { :small => "150x150>" },
:url => "/images/:style/:id/:basename.:extension",
:path => ":rails_root/public/images/:style/:id/:basename.:extension"
before_validation :download_remote_image, :if => :image_url_provided?
private
def image_url_provided?
!self.image_url.blank?
end
def download_remote_image
self.image = do_download_remote_image
self.image_remote_url = image_url
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
end
我该怎么办?
- 创建一个新的图像模型,如“Photo2 模型”,然后复制并粘贴照片模型中的代码?
或更改关联,以便公司有很多照片。然后我只是不知道我应该如何跟踪 2 个不同的图像以及路由应该如何。
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 paperclip