6

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.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
Drew
  • 2,601
  • 6
  • 42
  • 65
  • 1
    Try this `<%= f.file_field :image, multiple: true, name: "attachment[file][]" %>` . – hawk Aug 08 '13 at 16:49
  • Ahh yes. Thanks! I will need to modify how I am saving the attachments but that will do just fine. Feel free to post as the answer and I will accept. – Drew Aug 08 '13 at 17:24
  • It wouldn't be f. in a real life example since f would have represented the top level of the form and your posted code doesn't even have a fields_for statement If your question is about nested forms why don't you include the fields_for statement in your question's code? I have to ask, is the file_field nested? – Scott K Feb 03 '23 at 20:40

1 Answers1

12

Just append [] to your params

<%= f.file_field :image, multiple: true, name: "attachment[file][]" %>
hawk
  • 5,409
  • 2
  • 23
  • 29