Overriding Paperclip Defaults for your Entire Rails App

This entry was posted February 18 2009 under Rails, Web Development.

1 comment

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 => :o 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.

Comments

This is way old post, but it's risky to override the defaults entirely, you're safer doing something like this in your initializer:

Paperclip::Attachment.default_options.merge!({:default_url => "/:attachment/:style/whatevskyz.png")

Post a Comment







OMG Who?

I'm Sean Roberts, a 24-year-old Ruby on Rails developer currently working for Hamilton web design firm factor[e] design initiative. This is my blog about work and nerd stuff, and maybe a little bit about music and food too.

I'm On Twitter!

  • The motto for RubyGems should be "can't someone else do it?" 2010-10-25
  • New Superchunk makes me feel like I'm 17, anyone up for semi-ironically playing Mario Kart 64 and making Kraft Dinner? 2010-09-24
  • what an unfortunate (yet hilarious) name http://imgur.com/2biWk 2010-09-22
  • Hey Google, stop interrupting me when I'm trying to ask you a question! 2010-09-09
  • My new book will be called "Living in Synergy: Memoirs of an Unwed Power Couple" (with help from @EKinread of course) 2010-09-01
  • Can anyone recommend a good tailor in Hamilton? 2010-08-23
  • More updates...