Skip to content
Tutorial for Carrierwave multiply files upload
Ruby JavaScript CoffeeScript
Find file
Latest commit 7ec44fd Apr 30, 2012 @itsNikolay Merge pull request #1 from richardkmichael/master
Code syntax highlighting.
Failed to load latest commit information.
app Readme v0.9 and some changes Apr 1, 2012
config Added a root_path Mar 31, 2012
db -hidden field Mar 31, 2012
doc first commit Mar 31, 2012
lib first commit Mar 31, 2012
log first commit Mar 31, 2012
public All works! :+1: Mar 31, 2012
script first commit Mar 31, 2012
test first commit Mar 31, 2012
vendor first commit Mar 31, 2012
.gitignore first commit Mar 31, 2012
.project first commit Mar 31, 2012
Gemfile first commit Mar 31, 2012
Gemfile.lock first commit Mar 31, 2012
README.markdown Thanks for writing this up. I quickly added a few references and gave… Apr 28, 2012
Rakefile first commit Mar 31, 2012
config.ru first commit Mar 31, 2012

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.