How to Upload a File in Rails using CarrierWave

How to Upload a File in Rails using CarrierWave

2 Minutes Read

Uploading files in a Rails App is no sweet job now a days. Out of all the gems available in Rails Ecosystem, “CarrierWave” is widely used for its easy integration and flexibility to upload files.


  • File Caching: Prevents users to select the file again & re-upload if the form fails the validation process
  • Clean Code: Helps to write all the logic in uploader classes rather than in models, keeping your model clean and readable
  • Image Processing: Provides efficient way to resize & crop images to different formats
  • Storage Support: Stores files in third party storages such as AWS S3 bucket, Racksapce or Google Cloud
  • Mongodb: Has good support for mongodb with the help of “carrierwave-mongoid” gem

Setting Up CarrierWave
Step 1:
In your Rails App, add the following to your gem file & run the bundle installation.

[code language=”html”]
gem ‘carrierwave’

Step 2:
Generate the uploader by running the following command:

[code language=”html”]
rails generate uploader Avatar

It gives you a file in the path given below:

[code language=”html”]

Here, “Avatar” is the column name.

Step 3:
Create a column to your model where you want to store the image:

[code language=”html”]
rails g migration add_avatar_to_students avatar:string

Step 4:
Open up the model and mount the uploader:

[code language=”html”]
class Student < ActiveRecord::Base
mount_uploader:avatar, AvatarUploader

Step 5:
In the generated uploader file you can mention different versions of an image, just like this:

[code language=”html”]
version :thumb do
process :resize_to_fill => [150, 150]
version :tiny_thumb do
process :resize_to_fill => [50, 50]

Step 6:
Set file storage path in the uploader file. It’s the default storage path where all the files will be stored by CarrierWave.

[code language=”html”]
def store_dir

Step 7:
Navigate to config > initializers and create a file: carrier_wave.rb.
Paste the following code:

[code language=”html”]
require ‘carrierwave/orm/activerecord’

At this point, it loads CarrierWave after loading ActiveRecord.
You can also store the files into AWS S3 bucket by configuring in this way:

[code language=”html”]
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => ‘AWS’,
: aws_access_key_id => "YOUR AMAZON ACCESS KEY",
:aws_secret_access_key => "YOUR AMAZON SECRET KEY",
:region => ‘us-west-1’ # Change this for different AWS region. Default is ‘us-east-1’
config.fog_directory = "bucket-name"

Now, you should be able to upload files on the fly after reading this article.

Wrapping Up!

Faced any hiccups while uploading a file using CarrierWave? Write to me at and I’ll try to find a way.

Also, I would appreciate if you leave your feedback/suggestions below.

Jayadev Das

Do what you do best in – that’s what I’ve always believed in and that’s what I preach. Over the past 25+ years (yup that’s my expertise ‘n’ experience in the Information Technology domain), I’ve been consulting to small, medium and large companies ‘bout Web Technologies, Mobile Future as well as on the good-and-bad of tech. Blogger, International Business Advisor, Web Technology Expert, Sales Guru, Startup Mentor, Insurance Sales Portal Expert & a Tennis Player. And top of all – a complete family man!

No Comments

Post A Comment

Exit pop up

Sad to see you leaving early...

From "Aha" to "Oh shit" we are sharing everything on our journey.
Enter your email address to stay up to date with the latest news.
Holler Box