Skip to content
This repository has been archived by the owner on Jul 19, 2022. It is now read-only.

itsNikolay/carrierwave_multiply_files_upload

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Tutorial for Carrierwave multiply files upload

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published