Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi select file upload field with nested attributes in Rails

Currently I have a Note model which accepts nested attributes for an Attachments model, which uses Carrierwave. When adding a Note, I have a nested form to allow attaching file to the new Note:

Nested form field:

<%= f.file_field :image, multiple: true, name: "attachment[file]" %>

I am using the Cocoon gem to add the nested field. While I can easily let them add multiple file upload fields with Cocoon, and add multiple attachments that way, I only want to load one file upload field, and let them use multi select to select multiple images.

When I do this, the file upload field says '2 Images' next to it. However, upon form submission, only one file is listed under 'attachments_attributes'. I need all of the attachments to submit at once as the Note has not yet been saved.

What is the proper way to accomplish this? I am aware of the Railscast on this topic however it did not seem to address my particular scenario.

Any help is appreciated.

like image 888
Drew Avatar asked Aug 08 '13 16:08

Drew


1 Answers

Just append [] to your params

<%= f.file_field :image, multiple: true, name: "attachment[file][]" %>
like image 135
hawk Avatar answered Oct 14 '22 08:10

hawk