【问题标题】:Paperclip separate styles回形针单独样式
【发布时间】:2016-06-03 21:56:56
【问题描述】:

如何区分不同型号的样式?

我有我的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'}

有可能吗?

【问题讨论】:

  • 请提供型号代码
  • @МалъСкрылевъ 已更新
  • 码装回形针在哪里?

标签: ruby-on-rails paperclip


【解决方案1】:

我建议您使用 STI 和多态性按图像模型类型分隔图像,因此,将 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 会更好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 2015-06-13
    • 1970-01-01
    相关资源
    最近更新 更多