Rails 4 + carrierwave/paperclip + nested model upload multiple images solution
Step 1) rails new multiple_image_upload_carrierwave
Step 2) In gem file
gem 'carrierwave'
Step 3) bundle install
Step 4) rails generate uploader Avatar
Step 5) Create post scaffold
Step 6) rails g scaffold title:string
Step 7) Create post_attachment scaffold
Step 8) rails g scaffold post_attachment post_id:integer avatar:string
Step 9) rake db:migrate
Step 10) In post.rb
class Post < ActiveRecord::Base
has_many :post_attachments accepts_nested_attributes_for :post_attachments
end
Step 11) In post_attachment.rb
class PostAttachment < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader belongs_to :post
end
Step 12) In post_controller.rb
def show
@post_attachments = @post.post_attachments.all
end
def new
@post = Post.new
@post_attachment = @post.post_attachments.build
end
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
params[:post_attachments]['avatar'].each do |a|
@post_attachment = @post.post_attachments.create!(:avatar => a, :post_id => @post.id)
end
format.html { redirect_to @post, notice: 'Post was successfully created.' }
else
format.html { render action: 'new' }
end
end
end
private
def post_params
params.require(:post).permit(:title, post_attachments_attributes: [:id, :post_id, :avatar])
end
Step 13) In views/posts/_form.html.erb
<%= form_for(@post, :html => { :multipart => true }) do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.fields_for :post_attachments do |p| %>
<%= f.text_field :title %>
<%= p.label :avatar %>
<%= p.file_field :avatar, :multiple => true, name: "post_attachments[avatar][]" %>
<% end %>
<%= p.file_field :avatar, :multiple => true, name: "post_attachments[avatar][]" %>
<%= f.submit %>
<% end %>Step 14 ) In views/posts/show.html.erb
<%= notice %>
Title:
<%= @post.title %>
Step 15)
<% @post_attachments.each do |p| %>
<%= image_tag p.avatar_url %>
<% end %>
<%= link_to 'Edit', edit_post_path(@post) %> | <%= link_to 'Back', posts_path %>
In rails 3 no need to define strong parameters and as you can define attribute_accessible in both the model and accept_nested_attribute to post model because attribute accessible is deprecated in rails 4.
No comments:
Post a Comment