The Original Jelani Harris The one and only


How to install Paperclip in Rails 3

The thoughtbot logo

Paperclip is a plugin/gem created by the talented folks at ThoughtBot. It will make using attachments and uploads in your rails application ridiculously easy to implement. Let's get started!

Things you'll need installedĀ 

Before we start there is only one thing that you will need to have installed on your system before we can proceed.

ImageMagik is a software suite that is used to edit and create images. Paperclip uses it to resize and modify images. The easiest way to install this is to first make sure that you have MacPorts installed, and then running:

sudo port install ImageMagick

This is a massive library and it will take a little while to install.

Installing Paperclip

So you can install Paperclip as a plugin, or you can install it as a gem.

Install as a Gem
This is the recommended way of installing Paperclip. All you need to do is to add to your config/environment.rb

config.gem 'paperclip', :source => ''

Then run:

rake gems:install
rake gems:unpack

Or you can add it directly to your Gemfile if you have Bundler:

source ''
gem 'paperclip'

Install as a Plugin
To install it as a plugin you use:

ruby script/plugin install git://

Using Paperclip in Your Application

Lets add the 'has_attached_file' attributes to the model we want to be able to attach files to:

class Author > ActiveRecord::Base
  has_attached_file :avatar,
    :styles => {
      :thumb => "75x75#",
      :small => "100x100#",
      :medium => "150x150>"

Thankfully attached files do not need to have a separate model. Your attachments are essentially treated like another attribute. The image is not saved until your model is saved (if you desire, there are ways of forcing attachment creation/updates without model involvement - but that's another tutorial)

Now that we have our model paperclip enabled we need to add some database columns to provide full support for it.

class AddAvatarToAuthor > ActiveRecord::Migration
  def self.up
    add_column :author, :avatar_file_name, :string
    add_column :author, :avatar_content_type, :string
    add_column :author, :avatar_file_size, :integer
  def self.down
    remove_column :author, :avatar_file_name
    remove_column :author, :avatar_content_type
    remove_column :author, :avatar_file_size

So the first part of the column name is the same as what we called our attachment attribute in our model. In this case that's photo. Now to update our database we use:

rake db:migrate

Now that we have our database and our model taken care of, we can start working on our content. So in our view we can add a file field:

<% form_for :author, :html => {:multipart => true} do |f| %>
  <%= f.label :avatar %><br />
  <%= f.file_field :avatar %>
  <%= f.submit %>
<% end %>

Displaying Attachments
Now when you want to display your model's attachments all you need to do is use:

<%= image_tag @author.avatar.url %>
<%= image_tag @author.avatar.url(:thumb) %>
Tagged as: , 5 Comments

Old domain, new language

So I've been the proud owner of a new website that I was surprised to get: I've actually had it for awhile and nothing is there except an outdated version of WordPress, and a blank hello world page. I'm surprised that it hasn't been spammed (or hacked) to hell now. But now I need to do something with it.

I was considering making it a personal art portfolio, but now I'm leaning towards making it an art community where people share and critique each other's artwork. The only thing I'm concerned with is that I don't want people stealing art from deviantArt and claiming them as their own and having people complain to me about it. It's just one of those things I sorta don't want to deal with, and that I know I'm going to deal with when Apparatus Complex getsĀ  popular in the future - when it's complete.

Of course before I actually go through with this community website I have to look at all of the "competition" and see what they do right and what they do wrong and then capitalize on those issues. That's business and research I suppose. Having facts and history and repeating it doesn't guarantee that a site will be popular.

Also, I do plan on creating this website in a new language. I've heard a lot of good things about Ruby on Rails (and not-so good things). I also just found out that my webhost, Site5, completely supports rails so there's no reason now why I shouldn't be learning this popular and new language.

The best way that I learn a new language is to have a project - or a goal - in mind so that I can visualize what my goal is and what I take the steps I need to do to get there. I'm not really one of those programmers that can just do all of the examples in a book and claim that I know the language. I have to get really deep into the planning of the project, and notice the parallels between the new language I'm learning and the old languages I already know. Like just skimming through some sample code, I notice that the way Ruby is structured it looks like Python with Java sprinkles. Or maybe, more like Java Flakes cereal with Ruby marshmallows.

(That actually sounds kinda delicious. I'm in the mood for Lucky Charms now.)

They say that once you learn/master one programming language, all of the other languages you want to learn get easier. I think that's 100% true.

Tagged as: , No Comments