Reputation: 1135
I am trying to modify my image upload routine in order to upload multiple images at the same time. Currently I am able to upload multiple images, but it creates only one record. How can I achieve it that one submit creates multiple records?
I do not want to go through an association, but instead be able to directly upload multiple images.
I currently have a Model:
class Diapo < ApplicationRecord
belongs_to :album
mount_uploaders :file_name, DiapoUploader
end
My Controller:
class Admin::DiaposController < ApplicationController
def new
@diapo = Diapo.new
@page_title = 'Create new Diapo'
end
def create
@diapo = Diapo.new(diapo_params)
if @diapo.save
flash[:notice] = 'Diapo #{@diapo.file_name} was successfully created.
redirect_to action: :index
else
@page_title = 'Create new Diapo'
render action: :new
end
end
.
.
.
private
def diapo_params
params.require(:diapo).permit({file_name: []}, :title, :alt, :author, :copyright, :album_id)
end
end
in the form I have:
<%= f.file_field :file_name, multiple: true, class: 'form-field' %>
Currently I get one post
Started POST "/admin/diapos" for ::1 at 2019-12-18 10:45:19 +0100
Processing by Admin::DiaposController#create as HTML
Parameters: {"authenticity_token"=>"something==", "diapo"=>{"file_name"=>[#<ActionDispatch::Http::UploadedFile:0x00007f9062b51a40 @tempfile=#<Tempfile:/var/folders/yg/pfjwzpkx5wq9d27svk760h0h0000gn/T/RackMultipart20191218-2329-7kffxo.jpg>, @original_filename="1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"diapo[file_name][]\"; filename=\"1.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x00007f9062b519f0 @tempfile=#<Tempfile:/var/folders/yg/pfjwzpkx5wq9d27svk760h0h0000gn/T/RackMultipart20191218-2329-o5qzhe.jpg>, @original_filename="2.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"diapo[file_name][]\"; filename=\"2.jpg\"\r\nContent-Type: image/jpeg\r\n">], "alt"=>"Test", "author_ids"=>"", "album_id"=>"124", "copyright"=>"Test"}, "commit"=>"Save"}
Admin Load (0.4ms) SELECT 'admins'.* FROM 'admins' WHERE 'admins'.'id' = 1 ORDER BY 'admins'.'id' ASC LIMIT 1
Unpermitted parameter: :author_ids
(0.2ms) BEGIN
↳ app/controllers/admin/diapos_controller.rb:14:in `create'
Album Load (0.3ms) SELECT `albums`.* FROM `albums` WHERE `albums`.`id` = 124 LIMIT 1
↳ app/controllers/admin/diapos_controller.rb:14:in `create'
Diapo Create (0.2ms) INSERT INTO `diapos` (`file_name`, `alt`, `copyright`, `album_id`, `created_at`, `updated_at`) VALUES ('[\"1.jpg\", \"2.jpg\"]', 'Test', 'Test', 124, '2019-12-18 09:45:34.406410', '2019-12-18 09:45:34.406410')
↳ app/controllers/admin/diapos_controller.rb:14:in `create'
(6.5ms) COMMIT
↳ app/controllers/admin/diapos_controller.rb:14:in `create'
Redirected to http://localhost:3000/admin/diapos
Completed 302 Found in 15350ms (ActiveRecord: 7.5ms | Allocations: 75512)
and 1 record is created which has the array of filenames in the filename field.
I suspect I can do it either in the controller or through javascript? How would I do this?
Thank you in advance!
Upvotes: 2
Views: 1689
Reputation: 2074
You have to implement your requirement with has_many relation. To save multiple record you may have parent child relationship just like below.
Ex :- Post is a parent table and save its images as post_attachments which is child table
class Post < ActiveRecord::Base
has_many :post_attachments
accepts_nested_attributes_for :post_attachments
end
class PostAttachment < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
belongs_to :post
end
this is how you can achieve to save multiple files for parent record
For more detail see this Article which may be helpful to you.
https://kolosek.com/carrierwave-upload-multiple-images/
Upvotes: 2