Paperclip set default images for all different styles
Asked Answered
S

1

7

So in my model I have different sizes for my avatar:

has_attached_file :avatar,
  :styles => {
    :thumb => "60x60>",
    :small  => "80x80>",
    :medium => "140x140>",
    :large =>   "300x300>"},
  :default_url => "/images/default_avatar.png"

Now when I call model_instance.avatar.url(:small) it will return "/images/default_avatar.png" if there is no avatar. It will also return the same result for any style I choose.

I want to make the default images change upon change of style so that when I call model_instance.avatar.url(:small) it should return "/images/default_avatar_small.png" and if I call model_instance.avatar.url(:large) it should return "/images/default_avatar_large.png"

How can I make paperclip assign different default avatar for each style?

Schmaltzy answered 15/9, 2012 at 13:5 Comment(1)
Found the answer here #5988251Schmaltzy
E
4

The :style can be included in the default url string which allows you to make it dynamic:

has_attached_file :avatar,
  :styles => {
    :thumb => "60x60>",
    :small  => "80x80>",
    :medium => "140x140>",
    :large =>   "300x300>"},
  :default_url => "/images/default_avatar_:style.png"
Ender answered 20/11, 2015 at 2:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.