[英]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.