Uploading Files in Rails with Paperclip Gem


In this tutorial, you’ll learn how to upload files in a rails app. Paperclip is the gem that is we are going to use for uploading images. But the Paperclip gem is not the only option. There are also many other gems available like Carrierwave and now Active Storage(builtin in Rails 5.2). But for this demonstration, we will be using Paperclip. You will need to install Image Magick in your computer globally.

if you are using the MAC OS then type the following command in the terminal window.

brew install imagemagick

If you are using Linux then type the following command.

sudo apt-get install imagemagick -y

This will install image magick in your computer. Now we will deal with the paperclip. Create your rails app and open app directory (Folder). Now open the Gemfile in the code editor. Add the following code in your gem file.

gem "paperclip"

I mostly recommend the latest version of the paperclip. Now after the typing, open your terminal window and type the following command

bundle install

It will install paperclip gem specified in your gem file. Now all the dependencies are installed and ready for use.

Create you new form the image

<%= simple_form_for @user, url: users_path do |form| %>
  <%= form.input :avatar, as: :file %>
  <%= form.submit %>
<% end %>

Suppose you have the user model in your app and this user can add the image in his post. Now we have to define image properties in over user model. Type the following code in the model.

class User < ActiveRecord::Base
    has_attached_file :avatar, styles: { large: "600x600>, 
    medium: "300x300>", thumb: "100x100>" }, 
    default_url: "/images/:style/missing.png"
    validates_attachment_content_type :avatar, content_type: 

Now you have to add the column for the image in user migration table. Type the following command in the terminal window.

rails g model AddAvatarColumnsToUser

This will generate the new file inherit with the user table. We have to modify this file type

class AddAvatarColumnsToUsers < ActiveRecord::Migration
  def up
    add_attachment :users, :avatar

  def down
    remove_attachment :users, :avatar

You can see above we define two methods in this file.

Up method is for creating or uploading the image while the down method is for removing an image. You also have to migrate this file. Type in the terminal

rails db migrate

It will migrate the model file. So in last, you need to define the new avatar to user controller. Open the user controller file in your code window and type the following command.

def create
  @user = User.create(user_params)

def user_params  

In the above code, I only defined the create function. You will need to define all method like index, show, edit, destroy. 

While showing an image you can use the following helper methods for the image. Open the show page and you type the following according to your need

<%= image_tag @user.avatar.url(:large) %>
<%= image_tag @user.avatar.url(:medium) %>
<%= image_tag @user.avatar.url(:thumb) %>

In the above code, I used the attribute like large, medium, thumb. These are the different size of the image we defined above in the user model. You can customize the size of the image.

Now your image uploading system is ready for use. I hope this will help. If you have any questions or suggestions please write down in the comments below. 

Leave a Reply

Your email address will not be published. Required fields are marked *