Reputation: 157
I have created a gallery module with the help of this tutorial https://kolosek.com/carrierwave-upload-multiple-images/ and now I want to add one more image attribute has a master image to the same model. So I thot of creating one more uploader but I'm bit confused about how to call that the controller can anybody help me out?
My code looks like this:
MOdel Code:
class Image < ApplicationRecord
belongs_to :gallery
mount_uploader :image, ImageUploader
mount_uploader :avatar, AvatarUploader
end
class Gallery < ApplicationRecord
has_many :images
accepts_nested_attributes_for :images
end
gallery_controller.rb
class GalleriesController < AdminController
def index
@galleries = Gallery.all
end
def show
@images = @gallery.images.all
end
def new
@gallery = Gallery.new
@image = @gallery.images.build
end
def create
@gallery = Gallery.new(gallery_params)
respond_to do |format|
if @gallery.save
params[:images]['image'].each do |a|
@images = @gallery.images.create!(:image => a, :gallery_id => @gallery.id)
end
format.html { redirect_to @gallery, notice: 'Gallery was successfully created.' }
format.json { render :show, status: :created, location: @gallery }
else
format.html { render :new }
format.json { render json: @gallery.errors, status: :unprocessable_entity }
end
end
end
def gallery_params
params.require(:gallery).permit(:title, :details, :status, images_attributes:[:id, :gallery_id, :image, :avatar])
end
Image Controller code:
class ImagesController < AdminController
def image_params
params.require(:image).permit(:gallery_id, :slideshow_id,:image, :avatar)
end
end
form.html.erb
<%= form.fields_for :images do |p| %>
<div class="field">
<%= form.label :master_image, class: "col-2 col-form-label" %>
<%= form.file_field :avatar, :multiple => true, name: "images[avatar][]" %>
</div>
<% end %>
<%= form.fields_for :images do |p| %>
<div class="field">
<%= form.label :image, class: "col-2 col-form-label" %>
<%= form.file_field :image, :multiple => true, name: "images[image][]" %>
</div>
<% end %>
I am confused in modifing the gallery controller create part.
Upvotes: 2
Views: 1259
Reputation: 5609
You can add the images creation 2 times in the create
method if you have 2 uploaders
params[:images]['image'].each do |a|
@gallery.images.create!(:image => a, :gallery_id => @gallery.id)
end
params[:images]['avatar'].each do |a|
@gallery.images.create!(:avatar => a, :gallery_id => @gallery.id)
end
but i think it's a little bit verbose and it not really prevents if you don't want to upload any image in your form (return params[:images]['...']
nil)
Btw your form is not correct for the form.fields_for
. It's :
<%= form.fields_for :images_attributes do |p| %>
<div class="field">
<%= p.label :master_image, class: "col-2 col-form-label" %>
<%= p.file_field :avatar, :multiple => true, name: "images[avatar][]" %>
</div>
<% end %>
<%= form.fields_for :images_attributes do |p| %>
<div class="field">
<%= p.label :image, class: "col-2 col-form-label" %>
<%= p.file_field :image, :multiple => true, name: "images[image][]" %>
</div>
<% end %>
Upvotes: 1