Introduction

We often need to utilize image uploads in our application. Paperclip is a gem that allows us to do this. Paperclip is an extremely powerful tool that allows us to manipulate images in a number of different ways.

Installation

In order to utilize Paperclip, we must first install ImageMagick. ImageMagick is the backend tool that Paperclip uses to manipulate images. You can find the binary releases of ImageMagick at http://www.imagemagick.org/script/binary-releases.php. Many package managers also already include ImageMagick so that you can easily install it. For example if you have homebrew installed on OS-X you can run brew install imagemagick to install it.

Next, we must add Paperclip to our gemfile, open your gemfile and add the following line:

gem 'paperclip'

After that, run a bundle install:

bundle install

Paperclip is now installed and available for use.

Creating a New Model That Utilizes Paperclip

If you are updating an existing model please see the next section.

To create a new model, generate the model as normal, adding an image of type attachment. The image field name can be named anything, so feel free to name it something else if you want. See the following example code:


rails g model Post title:string body:string image:attachment

This will create the necessary fields for paperclip to work. t.attachment creates 4 database fields:


image_file_name # The original filename of the image.
image_content_type # The mime type of the image
image_file_size # The file size of the image
image_updated_at # The last updated date of the image.

Now we need to Add Paperclip Support to the model.

Updating an Existing Model

In order to use paperclip, a few additional database fields are required. Paperclip has a helper to add these database fields. Create a new migration for the model you wish to add Paperclip to, in this case, our model is called 'Post':


rails g migration AddPaperclipToPost

Next, open the migration and add the following code:

add_attachment :posts, :image  

The add_attachment method is a helper that will automatically add the 4 fields that Paperclip uses:


image_file_name # The original filename of the image.
image_content_type # The mime type of the image.
image_file_size # The file size of the image.
image_updated_at # The last updated date of the image.

Adding Paperclip Support

Now that we've added the necessary fields to our model, we must add support for paperclip itself. Open your model and add the following code:


has_attached_file :image, styles: { small: "64x64", med: "100x100", large: "200x200" }

The styles that are defined are widths/heights and can be customized to fit your needs. For instance you could create a style called "icon" and make it "32x32".

Creating or Updating Your Views

Now that our model is set up we need to update our views to support paperclip. First, update your form tag to include html: { multipart: true } like this:


<= form_for @post, html: { multipart: true } do |f|

Next, in your show view, add the following code:


<%= link_to image_tag(@post.image.url(:med)), @post.image.url %>

You can also use the the other styles you defined such as :small or :large as well.

Validations

Validating attachments is easy. You can require an image be present:


validates :avatar, :attachment_presence => true

The file size:


validates_attachment :image, :size => { :in => 0..100.kilobytes }

Or the type:


validates_attachment :image, :content_type => { :content_type => "image/jpg" }

Or any combination of the the above validations:


validates_attachment :image, :presence => true,
  :content_type => { :content_type => "image/jpg" },
  :size => { :in => 0..100.kilobytes }

Conclusion

Paperclip makes it really easy to manipulate images within your projects. Thanks for reading!