Cyber Monday Sale Save up to 40% off unlimited courses, tutorials and creative assets. Cyber Monday Sale! Save Now
  1. Code
  2. Ruby on Rails

Rails Image Upload: Using CarrierWave in a Rails App


If you are building a web application, you definitely will want to enable image uploading. Image uploading is an important feature in modern-day applications, and images have been known to be useful in search engine optimization.

In this tutorial (which is the first part of the Rails Image Uploading series), I will show you how to enable image uploading in your Rails application using CarrierWave. It will be a simple application as the focus is on the image uploading.

CarrierWave is a Ruby gem that provides a simple and extremely flexible way to upload files from Ruby applications. You need to have Rails on your machine to follow along. To be sure, open up your terminal and enter the command below:

That will show you the version of Rails you have installed. For this tutorial I will be using version 4.2.4, which you can install like so:

With that done, you are good to go.

Rails Application Setup

Now create a new Rails project:

Open up your Gemfile and add the following gems.

The first gem is for CarrierWave, and the second gem called mini_magick helps with the resizing of images in your Rails application. With that done, run bundle install.

Generate a scaffold resource to add CarrierWave’s functionality. Run the following command from your terminal:

A scaffold in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.

Migrate your database next:

Setting Up CarrierWave

You need to create an initializer for CarrierWave, which will be used for loading CarrierWave after loading ActiveRecord.

Navigate to config > initializers and create a file: carrier_wave.rb.

Paste the code below into it.

From your terminal, generate an uploader:

This will create a new directory called uploaders in the app folder and a file inside called image_uploader.rb. The content of the file should look like this:

You can edit it to fit what you want. Let me take you through it.

First, uncomment the MiniMagick line. That should be line 7.

You need this to generate different versions of an image. If you want to generate a thumbnail version of images uploaded, there is already a code form included in the image_uploader file for you. Uncomment the version code block as shown below:

You can also add different versions using the same format.

For the purpose of this tutorial, we will be saving to file and not fog. Fog is the Ruby cloud service library. I will show you how to put it into use in another part of this series. So leave your storage option as it is.

For security purposes, certain files might pose a threat if allowed to be uploaded to the wrong location. CarrierWave allows you to specify a white-list of allowed extensions. You should see a method that looks like what I have below, so uncomment it.

It is time to mount your uploader. Navigate to your model and paste the code below.

Go to your views and edit it to look like what I have below:

Open your terminal and start your rails server: rails s.

Point your browser to http://localhost:3000/pets. You should be able to add a new pet by entering a name and description and uploading an image. The image does not get displayed after successful upload. Let me show you how to fix that.

Navigate to your show page where you are displaying the image, and edit it to fit what I have below:

This will display the thumbnail version of the image.

CarrierWave makes it easy to remove a previously uploaded file on a mounted uploader with just a checkbox. I will show you how to do it.

Open up your form file and make a little adjustment. Edit it to look like this:

In the code above, we checked if there is already an image object. If there is, we display the image and the option to remove it, but if there is none, we display just the field to upload the image.

Navigate to your controller and add :remove_image to your params. Reload your edit page, tick the box, click on Update Pet, and the image will be removed.

Validating Image Size

There are different means of doing this. I will show you an easy and quick method. Open up your pet model and paste in the code below:

This will help ensure that no image greater than 500KB gets uploaded to your Rails application. Start your rails server and check out what you have.


Now you know how to enable image uploading in your Rails application. You have also learned how to validate the format and size, as well as deleting an image. In the next part of this series, we will look at how to use CarrierWave alongside Devise.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.