[英]Paperclip separate styles
如何区分不同型号的款式?
我有可以生成样式的Photo model
large:'300x300#',huge:'800x800'
我也有两个女巫使用这个样式
Product
和
Post
所以我想用
large style
仅适用于Product
和huge style
只为Post
product => has_many :photos
post => has_one :photo
photo => belongs_to :post
belongs_to :product
has_attached_file :image, :styles => {large:'300x300#',huge:'800x800'}
可能吗?
我建议你使用STI一个图像模型类型,多态separte图像,因此,加imageable_type
, imageable_id
和type
场照片模式,所以:
app / models / photo.rb :
class Photo < AR::Base
belongs_to :imageable, polymorphic: true
end
app / models / photos / large_photo.rb :
class LargePhoto < Photo
has_attached_file :image, :styles => { large:'300x300#' }
end
app / models / photos / huge_photo.rb :
class HugePhoto < Photo
has_attached_file :image, :styles => { huge:'800x800' }
end
app / models / product.rb :
class Product < AR::Base
has_many :large_photos, as: imageable
end
app / models / post.rb :
class Post < AR::Base
has_one :huge_photo, as: imageable
end
但对我而言,在这种情况下,最好使用carrierwave
而不是paperclip
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.