views:

28

answers:

2

Hello,

I'm using paperclip, and have several styles:

:styles => {:large => "300x300>", :medium => "150x150>", :small => "50x50>", :thumb => "30x30>" }

The issue is default_stype, only applies to one of the sizes... :default_style => :thumb, :default_url => url here....

How can I set default_stypes for each style type? so if I call: <%= image_tag @user.profile_pic.url(:large) %>

The LARGE style has a default_url?

Thanks

+1  A: 

I Would suggest to use

has_attached_file :xyz, :url  => "/assets/:id", :path => ":rails_root/assets/photos/:attachable_type/:attachable_id/:id/:basename_:style.:extension",
                  :styles => { :large => "300x300>", :medium => "150x150>", :small => "50x50>", :thumb => "30x30>"}

and to get the proper style

/assets/:id?style=:style

like localhost:3000/assets/10?style=medium

Note: attacheable_type, attachable_id are coming from the polymorphic relations..

Hope it helps...

rgds,

Kannan R

KannanR
Please format your code when you post answers (use 4 spaces to indent)
Ryan Bigg
Thanks for the tips Ryan... its done :)
KannanR
Thanks but I don't get how that allows me to set default IMAGES per style? There should be a default_profile_pic_large.png, default_profile_pic_medium.png, default_profile_pic_small.png, default_profile_pic_thumb.png. I'm not sure I'm following what the above solves for? I don't have a challenge getting a particular style, just having it be smart enough to go to a default per style. Thoughts?
AnApprentice
Default is supposed to be one style isn't it? If you are looking for something like if a user didn't upload the image, then in few place you want to use default large image, and someother places you want to use default small image, default thumb image... as specified by grocery :default_url => "/images/missing/default_profilepic_:style.jpg" should solve your issue
KannanR
A: 

It's fairly easy. Just create paperclip.rb in your /config/initializers and put something like this in there:

module Paperclip
  class Attachment
    def self.default_options
      @default_options ||= {
        :url               => "/system/:class/:id/:style_:filename",
        :path              => ":rails_root/public:url",
        :styles            => {},
        :processors        => [:thumbnail],
        :convert_options   => {},
        :default_url       => "/images/missing/:class_:attachment_:style.jpg",
        :default_style     => :original,
        :storage           => :filesystem,
        :whiny             => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails]
      }
    end
  end
end

This overrides the defaults. So you can go ahead and change :default_style to whatever you want.

Grocery