Paperclip on Rails 3
by James
You want to run Rails 3. You want to use the excellent Paperclip plugin to mess with your attached files. Not a problem.
It Just Works
Paperclip on Rails 3 now just works.
Back in February 2010, I wrote about how to use Paperclip on an early beta release of Rails 3 — back then, a few small hacks were required to get Paperclip running. Now Rails 3 is at beta 4, and it’s safe to assume that we’re as close as we can be to an official Rails release — minus a few last minute tweaks, of course — and both the framework itself and the Paperclip plugin are more stable and more compatible. The good news, then, is that it’s very easy to use the Rails 3 and Paperclip together.
How To Use Paperclip With Rails 3
These instructions are good for Rails 3 beta 4, and Paperclip 9223a917. They’ll probably work on other versions of Rails and Paperclip, too.
- Install Rails 3
- Create a new Rails app
- Install Paperclip
- Create a model
- Create a controller and views
- Define your routes
You can find the important files (controller, model, views, and routes) in this gist, and the full application code is available on Github.
Install Rails 3
Install the latest prerelease of Rails with the following:
$ gem install rails --pre
Create a new Rails app
The command for creating Rails apps has changed in Rails 3. In the bright new future, everything is done using “rails” 1:
$ rails new paperclip_example
Install Paperclip
Paperclip’s master branch is now good to go with Rails 3. Install from Github thus:
$ rails plugin install git://github.com/thoughtbot/paperclip.git
Create a model
In order to work with Paperclip, your model needs a few special database columns. In this case, I’m creating a User class which will have an attached avatar image.
$ rails generate model User \ avatar_file_name:string \ avatar_content_type:string \ avatar_file_size:integer \ avatar_updated_at:datetime $ rake db:migrate
I use Paperclip’s has_attached_file method to define my avatar attachment:
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => {
:thumb => '50x'
}
end
Create a controller and views
I’m setting up a simple UsersController with index, new, and create actions (and corresponding routes). You’ll probably want to go further, but if all you’re interested in is proof-of-concept then this is enough.
$ rails generate controller Users
See this gist for the code in my UsersController, and the HTML in my views.
Define your routes
In config/routes.rb:
PaperclipExample::Application.routes.draw do |map| resources :users, :only => [:index, :new, :create] root :to => 'users#index' end
Lastly, if you’re mapping your app’s root to a controller, remember to delete public/index.html.
And You’re Done
That’s really all there is to it. Use rails server to start your app and view the results. Did it work for you?
Notes:
- I’ve found that, on Mac OSX 10.6, I have to use “/usr/bin/rails” (that’s where my Rails binary lives) instead of “rails”. No doubt that will be fixed soon. ↩
I just upgraded from Rails3.0.0.beta4 to RC1, and paperclip stopped working. Have you experienced any issues with RC1?
Charlie: it worked fine for me (I just tested with Rails 3 RC 1 and Paperclip at b5aea8b1).
Maybe you could post some more details about your problems?