public
Description: Paperclip File Management Plugin
Homepage: http://www.thoughtbot.com/projects/paperclip
Clone URL: git://github.com/thoughtbot/paperclip.git
jyurek (author)
Wed Oct 15 13:22:37 -0700 2008
commit  567657cbd6dfa49e931e81ee105a2f5599af3600
tree    df0dcbf04fa3f1402e8547741b13e2c2f7abb544
parent  339326dea09ab87c887d0ec9163030d07f6bf32a
name age message
file .gitignore Fri May 02 13:34:39 -0700 2008 Added a validation to validate against the cont... [austin.bain]
file LICENSE Fri Apr 18 09:08:33 -0700 2008 Moved the License into its own file [jyurek]
file README.rdoc Fri Sep 12 11:27:52 -0700 2008 Added a Contributing section to the README [jyurek]
file Rakefile Tue Jul 08 14:37:48 -0700 2008 Changed tests from test_*.rb to *_test.rb [jyurek]
directory generators/ Thu Aug 21 08:02:32 -0700 2008 Fixed missing .erb in generator [jyurek]
file init.rb Fri Apr 18 09:08:54 -0700 2008 Rearranged stuff and added a gem build/deploy p... [jyurek]
directory lib/ Thu Nov 13 08:38:44 -0800 2008 Changed Geometry#to_s to include the modifier s... [Steve]
directory shoulda_macros/ Wed Oct 15 13:22:14 -0700 2008 Changed symbol to string for method name compar... [jyurek]
directory tasks/ Wed Nov 05 13:32:14 -0800 2008 Added patch from Tekin Suleyman regarding prima... [jyurek]
directory test/ Fri Nov 14 14:45:31 -0800 2008 Tweaks to geometry changes. [jyurek]
README.rdoc

Paperclip

Paperclip is intended as an easy file attachment library for ActiveRecord. The intent behind it was to keep setup as easy as possible and to treat files as much like other attributes as possible. This means they aren’t saved to their final locations on disk, nor are they deleted if set to nil, until ActiveRecord::Base#save is called. It manages validations based on size and presence, if required. It can transform its assigned image into thumbnails if needed, and the prerequisites are as simple as installing ImageMagick (which, for most modern Unix-based systems, is as easy as installing the right packages). Attached files are saved to the filesystem and referenced in the browser by an easily understandable specification, which has sensible and useful defaults.

See the documentation for the has_attached_file method for options.

Usage

In your model:

  class User < ActiveRecord::Base
    has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
  end

In your migrations:

  class AddAvatarColumnsToUser < ActiveRecord::Migration
    def self.up
      add_column :users, :avatar_file_name,    :string
      add_column :users, :avatar_content_type, :string
      add_column :users, :avatar_file_size,    :integer
      add_column :users, :avatar_updated_at,   :datetime
    end

    def self.down
      remove_column :users, :avatar_file_name
      remove_column :users, :avatar_content_type
      remove_column :users, :avatar_file_size
      remove_column :users, :avatar_updated_at
    end
  end

In your edit and new views:

  <% form_for :user, @user, :url => user_path, :html => { :multipart => true } do |form| %>
    <%= form.file_field :avatar %>
  <% end %>

In your controller:

  def create
    @user = User.create( params[:user] )
  end

In your show view:

  <%= image_tag @user.avatar.url %>
  <%= image_tag @user.avatar.url(:medium) %>
  <%= image_tag @user.avatar.url(:thumb) %>

Contributing

If you’d like to contribute a feature or bugfix, thanks! To make sure your fix/feature has a high chance of being added, please read the following guidelines:

  1. Ask on the mailing list, or post a ticket in Lighthouse.
  2. Make sure there are tests! I will not accept any patch that is not tested. It’s a rare time when explicit tests aren’t needed. If you have questions about writing tests for paperclip, please ask the mailing list.