Adding Photo Upload to a Rails 3.2 App with Carrierwave

Posted on Nov 13, 2012 in Blog

At Suntoucher we manage our file uploads with the carrierwave gem.  We store the files on S3 because we deploy to Heroku. The fog gem manages the integration with S3. We use the rmagick gem for photo processing.

Here is a step by step guide to implement file uploading in a rails 3.x application.

1. Install Imagemagick

brew install imagemagick

2. Install gems

gem 'carrierwave'
gem 'fog'
gem 'rmagick'

3. Generate uploader

rails g uploader Photo

4. Create buckets on S3

We create three buckets on S3: production, staging and dev buckets.  For test we store on the file system.

5. Setup carrierwave, fog and S3 configuration in intializer

create config/initializers/carrierwave.rb 

6. Mount uploader in your model

mount_uploader :photo, PhotoUploader

Don’t forget to add the photo and photo_cache attributes to your attr_accessible.

7. Add photo fields to the model you just mounted the uploader to

rails g migration AddPhotoFieldToLocation photo:string
rake db:migrate

8. Add upload fields to the view

Don’t forget the :multipart => true.