In case the Intervention
model is only used to facilitate creating many Uploads (with one uploaded file each) at one time, there is a simpler way without an intermediate model.
My Document
model has has_one_attached :file
. I want to create multiple documents with one form which uploads multiple attachments. The controller specifies an array parameter without a corresponding db field:
params.require(:document).permit(..., multiple_files: [])
Note that this array parameter has to be the last item in the strong param list, otherwise you get syntax error.
The new-document form has f.file_field(:multiple_files, multiple: true)
.
The Documents#create
action has:
def create
params[:document][:multiple_files].each do |upload|
next unless upload.present?
@document = Document.new
@document.file.attach(upload)
# check for errors
@document.valid?
flash.now[:danger] = @document.errors.full_messages
# save, etc.
end
end