Overriding Paperclip Defaults for your Entire Rails App

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

No comments

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.

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!

  • Can anyone recommend a good tailor in Hamilton? 2 weeks ago
  • Alphabits are a highly underrated cereal. Just saying. 2 weeks ago
  • Oh look, another good deal on a product that I need and can't find at a store around here. Do they ship to Canada? Of course not. 3 weeks ago
  • RT @JayHiggins: I'm going to be positive: Games big success, Ticats sold to billionare, LRT, Condo Dev at WH, Bettman retires 3 weeks ago
  • Wait, Hamilton has a football team? 3 weeks ago
  • Hooray for west harbour! I promise to not be sarcastic RE: government for a couple days. Probably. 3 weeks ago
  • More updates...