Multi select file upload field with nested attributes in Rails

Drew picture Drew · Aug 8, 2013 · Viewed 8k times · Source

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.

Answer

hawk picture hawk · Aug 8, 2013

Just append [] to your params

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