Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Tutorial for Carrierwave multiply files upload

branch: master
README.markdown

CarrierWave nested_attributes_form and multiple image uploading tutorial

Created on 01.04.2012 (with gem CarrierWave v 0.6.0 https://github.com/jnicklas/carrierwave)

See also:

CarrierWave images will be located on model Article

$> rails g scaffold Article title body:text

Gemfile

gem 'carrierwave'
gem 'rmagick'
$> bundle

Model

$> rails g model Article_image image article_id
$> rake db:migrate
$> rails generate uploader Image

app/models/article.rb

class Article < ActiveRecord::Base

  # NOTE: If using attr_accessible, you must whitelist the nested attribute writer.
  # attr_accessible :title, :body, :article_images_attributes

  has_many :article_images, :dependent => :destroy  
  accepts_nested_attributes_for :article_images, :allow_destroy => true 
end

app/models/article_image.rb

class ArticleImages < ActiveRecord::Base

  # attr_accessible :image  
  mount_uploader :image, ImageUploader  
  belongs_to :article
end

Controller

app/controllers/articles_controller.rb

def show
  @article = Article.find(params[:id])
  @images = @article.article_images # Add this line (extract all article's images).
  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @article }
  end
end

def new
  @article = Article.new
  @article.article_images.build # Adding this line
   # 3.times { @article.article_images.build } # This line for multiple files with one action.
  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @article }
  end
end

def edit
  @article = Article.find(params[:id])
  @article.article_images.build # Adding this line
  # 3.times { @article.article_images.build } # This line for multiple files with one action.
end

View

app/views/articles/_form.html.erb

<%= form_for @article, :html => {:multipart => true} do |f| %>

  <%= f.fields_for :article_images do |article_image| %>
    <% if article_image.object.new_record? %>
      <%= article_image.file_field :image %>
    <% else %>
      <%= image_tag(article_image.object.image.url(:thumb)) %>
      <%= article_image.check_box :_destroy %>
    <% end %>
  <% end %>

<% end %> 

app/views/articles/show.html.erb

<% @article.article_images.each do |article_image| %>
  <%= link_to(image_tag(article_image.image.url(:thumb)), article_image.image.url) %>
<% end %>

Configuration depends on your needs

app/uploaders/image_uploader.rb

# Uncomment this line for image processing.
# include CarrierWave::RMagick

# Uncomment these lines to enable thumbnails, change size as required.
# version :thumb do 
#   process :resize_to_limit => [200, 200]
# end

http://localhost:3000/articles

Something went wrong with that request. Please try again.