Created on 01.04.2012 (with gem CarrierWave v 0.6.0 https://github.com/jnicklas/carrierwave)
See also:
- http://lucapette.com/rails/multiple-files-upload-with-carrierwave-and-nested_form/
- https://github.com/lucapette/carrierwave-nested_form
$> rails g scaffold Article title body:text
Gemfile
gem 'carrierwave'
gem 'rmagick'
$> bundle
$> 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
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
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 %>
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