簡體   English   中英

回形針單獨的樣式

[英]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_typeimageable_idtype場照片模式,所以:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM