Skip to content
Tutorial for Carrierwave multiply files upload
Ruby JavaScript CoffeeScript
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
app Readme v0.9 and some changes
config Added a root_path
db -hidden field
doc first commit
lib first commit
log first commit
public All works! :+1:
script first commit
test first commit
vendor first commit
.gitignore first commit
.project first commit
Gemfile first commit
Gemfile.lock first commit
README.markdown Thanks for writing this up. I quickly added a few references and gave…
Rakefile first commit
config.ru first commit

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.