Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paperclip set default images for all different styles

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?

like image 722
wael34218 Avatar asked Sep 15 '12 13:09

wael34218


1 Answers

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"
like image 130
Tom Rossi Avatar answered Oct 26 '22 14:10

Tom Rossi