我有一个多态的照片模型
class Photo < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
has_attached_file :image,
:styles => {:thumb => '120x120>', :medium => '640x480>' },
:default_style => :thumb,
:url => "/uploads/photos/#{model.imageable_type.to_s.pluralize.underscore}/:id/:basename.:extension"
和
class Question < ActiveRecord::Base
has_one :photo, as: :imageable, dependent: :destroy
class Answer < ActiveRecord::Base
has_one :photo, as: :imageable, dependent: :destroy
这会生成url as uploads / photos / Photo / some_id / file_name.ext 但是我想要
public/uploads/photos/questions/1/image.jpg
和
public/uploads/photos/answers/1/image.jpg.
答案 0 :(得分:1)
自己解决了。
:url => "/uploads/photos/:imageable_name/:id/:basename.:extension"
Paperclip.interpolates :imageable_name do |attachment, style|
attachment.instance.imageable_type.pluralize.downcase
end