Paperclip is intended as an easy file attachment library for Active Record. 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 has_attached_file
in Paperclip::ClassMethods
for
more detailed options.
The complete RDoc is online.
Paperclip now requires Ruby version >= 1.9.2 and Rails version >= 3.0 (Only if you're going to use Paperclip with Ruby on Rails.)
If you're still on Ruby 1.8.7 or Ruby on Rails 2.3.x, you can still use Paperclip 2.7.x with your project. Also, everything in this README might not apply to your version of Paperclip, and you should read the README for version 2.7 instead.
ImageMagick must be installed and Paperclip must have access to it. To ensure
that it does, on your command line, run which convert
(one of the ImageMagick
utilities). This will give you the path where that utility is installed. For
example, it might return /usr/local/bin/convert
.
Then, in your environment config file, let Paperclip know to look there by adding that directory to its path.
In development mode, you might add this line to config/environments/development.rb)
:
Paperclip.options[:command_path] = "/usr/local/bin/"
If you're on Mac OS X, you'll want to run the following with Homebrew:
brew install imagemagick
If you are dealing with pdf uploads or running the test suite, you'll also need GhostScript to be installed. On Mac OS X, you can also install that using Homebrew:
brew install gs
Paperclip is distributed as a gem, which is how it should be used in your app.
Include the gem in your Gemfile:
gem "paperclip", "~> 3.0"
If you're still using Rails 2.3.x, you should do this instead:
gem "paperclip", "~> 2.7"
Or, if you want to get the latest, you can get master from the main paperclip repository:
gem "paperclip", :git
8000
=> "git://github.com/thoughtbot/paperclip.git"
If you're trying to use features that don't seem to be in the latest released gem, but are mentioned in this README, then you probably need to specify the master branch if you want to use them. This README is probably ahead of the latest released version, if you're reading it on GitHub.
For Non-Rails usage:
class ModuleName < ActiveRecord::Base
include Paperclip::Glue
...
end
In your model:
class User < ActiveRecord::Base
attr_accessible :avatar
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
In your migrations:
class AddAvatarColumnsToUsers < ActiveRecord::Migration
def self.up
add_attachment :users, :avatar
end
def self.down
remove_attachment :users, :avatar
end
end
(Or you can use migration generator: rails generate paperclip user avatar
)
In your edit and new views:
<%= form_for @user, :url => users_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) %>
To detach a file, simply set the attribute to nil
:
@user.avatar = nil
@user.save
The basics of paperclip are quite simple: Declare that your model has an
attachment with the has_attached_file
method, and give it a name.
Paperclip will wrap up up to four attributes (all prefixed with that attachment's name, so you can have multiple attachments per model if you wish) and give them a friendly front end. These attributes are:
<attachment>_file_name
<attachment>_file_size
<attachment>_content_type
<attachment>_updated_at
By default, only <attachment>_file_name
is required for paperclip to operate.
You'll need to add <attachment>_content_type
in case you want to use content type
validation.
More information about the options to has_attached_file
is available in the
documentation of Paperclip::ClassMethods
.
For validations, Paperclip introduces several validators to validate your attachment:
AttachmentContentTypeValidator
AttachmentPresenceValidator
AttachmentSizeValidator
Example Usage:
validates :avatar, :attachment_presence => true
validates_with AttachmentPresenceValidator, :attributes => :avatar
Validators can also be defined using the old helper style:
validates_attachment_presence
validates_attachment_content_type
validates_attachment_size
Example Usage:
validates_attachment_presence :avatar
Lastly, you can also define multiple validations on a single attachment using validates_attachment
:
validates_attachment :avatar, :presence => true,
:content_type => { :content_type => "image/jpg" },
:size => { :in => 0..10.kilobytes }
Global defaults for all your paperclip attachments can be defined by changing the Paperclip::Attachment.default_options Hash, this can be useful for setting your default storage settings per example so you won't have to define them in every has_attached_file definition.
If you're using Rails you can define a Hash with default options in config/application.rb or in any of the config/environments/*.rb files on config.paperclip_defaults, these well get merged into Paperclip::Attachment.default_options as your Rails app boots. An example:
module YourApp
class Application < Rails::Application
# Other code...
config.paperclip_defaults = {:storage => :fog, :fog_credentials => {:provider => "Local", :local_root => "#{Rails.root}/public"}, :fog_directory => "", :fog_host => "localhost"}
end
end
Another option is to directly modify the Paperclip::Attachment.default_options Hash, this method works for non-Rails applications or is an option if you prefer to place the Paperclip default settings in an initializer.
An example Rails initializer would look something like this:
Paperclip::Attachment.default_options[:storage] = :fog
Paperclip::Attachment.default_options[:fog_credentials] = {:provider => "Local", :local_root => "#{Rails.root}/public"}
Paperclip::Attachment.default_options[:fog_directory] = ""
Paperclip::Attachment.default_options[:fog_host] = "http://localhost:3000"
Paperclip defines several migration methods which can be used to create necessary columns in your model. There are two types of method:
class AddAttachmentToUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.attachment :avatar
end
end
end
If you're using Rails 3.2 or newer, this method works in change
method as well:
class AddAttachmentToUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.attachment :avatar
end
end
end
class AddAttachmentToUsers < ActiveRecord::Migration
def self.up
add_attachment :users, :avatar
end
def self.down
remove_attachment :users, :avatar
end
end
If you're using Rails 3.2 or newer, you only need add_attachment
in your change
method:
class AddAttachmentToUsers < ActiveRecord::Migration
def change
add_attachment :users, :avatar
end
end
Vintage syntax (such as t.has_attached_file
and drop_attached_file
) are still supported in
Paperclip 3.x, but you're advised to update those migration files to use this new syntax.
Paperclip ships with 3 storage adapters:
- File Storage
- S3 Storage (via
aws-sdk
) - Fog Storage
If you would like to use Paperclip with another storage, you can install these gems along side with Paperclip:
The files that are assigned as attachments are, by default, placed in the
directory specified by the :path
option to has_attached_file
. By default, this
location is :rails_root/public/system/:class/:attachment/:id_partition/:style/:filename
.
This location was chosen because on standard Capistrano deployments, the
public/system
directory is symlinked to the app's shared directory, meaning it
will survive between deployments. For example, using that :path
, you may have a
file at
/data/myapp/releases/20081229172410/public/system/users/avatar/000/000/013/small/my_pic.png
NOTE: This is a change from previous versions of Paperclip, but is overall a safer choice for the default file store.
You may also choose to store your files using Amazon's S3 service. To do so, include
the aws-sdk
gem in your Gemfile:
gem 'aws-sdk', '~> 1.3.4'
And then you can specify using S3 from has_attached_file
.
You can find more information about configuring and using S3 storage in
the Paperclip::Storage::S3
documentation.
Files on the local filesystem (and in the Rails app's public directory) will be
available to the internet at large. If you require access control, it's
possible to place your files in a different location. You will need to change
both the :path
and :url
options in order to make sure the files are unavailable
to the public. Both :path
and :url
allow the same set of interpolated
variables.
Paperclip supports an extensible selection of post-processors. When you define
a set of styles for an attachment, by default it is expected that those
"styles" are actually "thumbnails". However, you can do much more than just
thumbnail images. By defining a subclass of Paperclip::Processor, you can
perform any processing you want on the files that are attached. Any file in
your Rails app's lib/paperclip_processors directory is automatically loaded by
paperclip, allowing you to easily define custom processors. You can specify a
processor with the :processors option to has_attached_file
:
has_attached_file :scan, :styles => { :text => { :quality => :better } },
:processors => [:ocr]