CarrierWave Upload Multiple Images

When you are building a web application, you definitely want to add an option for image uploading as well. In this tutorial, we will show you the necessary steps to enable upload multiple images/files using CarrierWave in Rails 4+ from scratch. A similar procedure can be applied for nested forms.

Step 1: In the gem file

CarrierWave is a Ruby gem that provides a simple and extremely flexible way to upload files from Ruby applications. You will need to add these gems to the Gemfile and run
bundle install
gem 'carrierwave'

Step 2: Setting up CarrierWave

The first step to configure CarrierWave it to run the following command:

rails generate uploader Avatar

This will create a new directory called uploaders in the app folder and a file inside called avatar_uploader.rb.

class AvatarUploader < CarrierWave::Uploader::Base
  storage :file
  def store_dir
  def default_url
    "/images/fallback/" + [version_name, "default.png"].compact.join('_')

– You can modify the default directory to change where uploaded files will be stored.


– It is the path to the default image, you can use this image if no other image has been selected.

Step 3: Generate your migrations

You can generate your migrations using Scaffolding in Rails. This will generate a full set of model, views and controller, and a test suite for each of the above. It is the most simple way to get your project up and running.

Step 4: Edit Models

Once scaffolding finishes generating the files you can start editing your models.

You want to be able to create post and post_attachment in one go. To do this you need to add the following to




    class Post < ActiveRecord::Base
     has_many :post_attachments
     accepts_nested_attributes_for :post_attachments
`accepts_nested_attributes_for :post_attachments`

Nested attributes allow you to save attributes on associated records through the parent.

The time has come to mount your uploader. Navigate to your PostAttachment model and add

`mount_uploader :avatar, AvatarUploader`

to the file.


    class PostAttachment < ActiveRecord::Base
       mount_uploader :avatar, AvatarUploader
       belongs_to :post

Step 5: Modify your Controller

The next step is to modify the


to accept those new nested attributes.


def show
  @post_attachments = @post.post_attachments.all
def new
  @post =
  @post_attachment =
To make sure that


is ready to be created once the new post is done, you will need to build it with



 def create
       @post =
       respond_to do |format|
           params[:post_attachments]['avatar'].each do |a|
              @post_attachment = @post.post_attachments.create!(:avatar => a,     :post_id =>
           format.html { redirect_to @post, notice: 'Post was successfully     created.' }
           format.html { render action: 'new' }

To help the nested attributes reach the model, you will need to add the following to the post_params method in the PostController.

def post_params
  params.require(:post).permit(:title, post_attachments_attributes: 
  [:id, :post_id, :avatar])

Step 6: View layout

For the last step to finish out application you will need to edit the view to allow us to upload new images. You can do this by adding the following line:

<%= p.file_field :avatar, :multiple => true, name:     "post_attachments[avatar][]" %>


<%= form_for(@post, :html => { :multipart => true }) do |f| %>
       <div class="field">
         <%= f.label :title %><br>
         <%= f.text_field :title %>
       <%= f.fields_for :post_attachments do |p| %>
         <div class="field">
           <%= p.label :avatar %><br>
           <%= p.file_field :avatar, :multiple => true, name:     "post_attachments[avatar][]" %>
       <% end %>
       <div class="actions">
         <%= f.submit %>
    <% end %>

Thank you for reading!

Previously published at

read original article here