How to Upload a File in Rails using CarrierWave

How to Upload a File in Rails using CarrierWave

Carrierwave

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.

Benefits

  • 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’
[/code]

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

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

It gives you a file in the path given below:

[code language=”html”]
app/uploaders/avatar_uploader.rb
[/code]

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
Rakedb:migrate
[/code]

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

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

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]
end
version :tiny_thumb do
process :resize_to_fill => [50, 50]
end
[/code]

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
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
[/code]

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

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

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"
end
[/code]

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 info@andolasoft.com and I’ll try to find a way.

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

Jay
Jayadev Das
jayadev.das@andolasoft.com

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!