Overriding Paperclip Defaults for your Entire Rails App
Paperclip is hands-down the best file attachment plugin for Rails. File_column was alright a while back, and attachment_fu was a frustrating mess. One thing I don’t like about Paperclip though is that by default it fills your public folder with uploads. (NOTE: As of the latest version of Paperclip this is no longer true.) This is even worse if you have an attachment called “images” as they just get dropped into your images folder.
While it’s fairly easy to redefine where the attachments get saved on a per-model basis with the :url and :path options, I very much prefer to do something once and then never have to think about it again. (This preference is part of the reason that this is only my second post on a blog I’ve had since December.) Thankfully, overriding the defaults for your entire app is easy. Just make a file called paperclip_defaults.rb in your config/initializers directory and fill it with the following lovely code:
module Paperclip
class Attachment
def self.default_options
@default_options ||= {
:url => "/system/:class/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/system/:class/:attachment/:id/:style/:basename.:extension",
:styles => {},
:default_url => "/:attachment/:style/missing.png",
:default_style =>
riginal,
:validations => [],
:storage => :filesystem
}
end
end
end
Just change the options in that block to suit your fancy, and the changes will take place across your entire application.
