Dynamic use of :default_url in Paperclip

前端 未结 4 1404
灰色年华
灰色年华 2020-12-16 17:31

I\'m trying to configure Paperclip to provide different missing images based on the instance\'s category attribute. Every category of the object has its own missing image.

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-16 18:06

    You can pass a Proc as :default_url to paperclip. See https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/attachment.rb#L135. Paperclip will call that proc with the Attachment object as a parameter. The Attachment object has an accessor 'instance' that is the ActiveRecord object instance it's attached to. In your case you should have:

      has_attached_file :logo,
                :path => "/:id-:style-:filename",
                :url  => ":s3_eu_url",
                :default_url => lambda { |attach| "/logos/:style/#{attach.instance.category.name]}.png },
                :styles => { :large => "600x400>",
                             :medium => "300x200>",
                             :small => "100x75>",
                             :thumb => "60x42>" }
    

提交回复
热议问题