matthewvb
matthewvb

Reputation: 942

Uploading multiple files in rails 3

I'm working on uploading multiple files but I'm not passing the params correctly between the form and the controller - not quite sure what I'm missing.

Form is pretty simple:

<%= form_tag({:controller => :admins, :action => :upload_json}, {:multipart => true, :method => :post, :html => { :class => "form-horizontal"}}) do %>

  <fieldset>
    <legend>Data</legend>
    <ol class="field-list">
      <li>
        <label for="data-file">JSON File</label>
        <%= file_field_tag 'jsonfileupload', multiple: true %>
      </li>
    </ol>


    <div class="form-actions">
      <%= submit_tag "Upload" %>
    </div>

  </fieldset>

<% end %>

In the controller, I'm just doing this to see what's passed:

@output = params[:jsonfileupload]

In the view, I just do a debug to see what's returned, and it only pulls the last file of any set of selected files.

#<ActionDispatch::Http::UploadedFile:0x007f95d0c21010 @original_filename="4987.json", @content_type="application/json", @headers="Content-Disposition: form-data; name=\"jsonfileupload\"; filename=\"4987.json\"\r\nContent-Type: application/json\r\n", @tempfile=#<File:/var/folders/0p/6lq88m950mgftng1qm1w63_8000194/T/RackMultipart20121114-389-t9l7vs>>

Not sure what I'm missing to get it to return all the files selected. Thanks for the help!

Upvotes: 1

Views: 2880

Answers (1)

matthewvb
matthewvb

Reputation: 942

I was able to solve this by adjusting the file_field_tag to: <%= file_field_tag 'jsonfileupload[]', multiple: true %>

Upvotes: 7

Related Questions