Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
Advertisement
  1. Code
  2. Ruby on Rails

Rails Image Upload: Using CarrierWave in a Rails App

Difficulty:BeginnerLength:ShortLanguages:

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:

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