Search code examples
ruby-on-railsfile-uploadruby-on-rails-7

Rails: Multiple Fileupload is missing all files but one


My Multiupload Form doesn't work as expected:

# app/views/activeadmin/dashboard/_import_cves.html.erb
<%= form_tag admin_dashboard_import_cves_path, method: :post, multipart: true do %>
  <%= file_field_tag 'files', multiple: true %>
  <%= submit_tag 'Import CVEs' %>
<% end %>
# app/admin/dhasboard.rb
ActiveAdmin.register_page "Dashboard" do
  menu priority: 1, label: proc { I18n.t("active_admin.dashboard") }

  page_action :import_cves, method: :post do
    files.each do |f|
      logger.debug f
    end
    redirect_to admin_dashboard_path
  end
  content title: proc { I18n.t("active_admin.dashboard") } do
    columns do
      column do
        panel 'Upload CVEs' do
          ul do
            render 'activeadmin/dashboard/import_cves'
          end
        end
      end
    end
  end
end

So... doing nothing special, yet. My form is displayed, I select multiple files (Chrome & Firefox on MacOS) and... Error:

undefined method `each' for #<ActionDispatch::Http::UploadedFile:0x0000000107415070 @tempfile=#Tempfile:/var/folders/6d/qr_wv8mx1zsgz0742b1j6p440000gn/T/RackMultipart20230203-9446-xlwanr, @original_filename="blc", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name="files"; filename="blc"\r\nContent-Type: application/octet-stream\r\n">

The thing is, I definitely selected 3 files. Which are at least in the filesystem in rails tempfolder:

-rw-------    1 havoc  staff     0B  3 Feb 14:53 RackMultipart20230203-9446-bzjcc2
-rw-------    1 havoc  staff     0B  3 Feb 14:53 RackMultipart20230203-9446-k0zm3
-rw-------    1 havoc  staff     0B  3 Feb 14:53 RackMultipart20230203-9446-xlwanr

(PS: yes, these testfiles are empty - just touched three of them - but happens with pictures/other files exactly the same)

Any Idea what's wrong here?

Maybe an additional note: I'm using, as you may suggest already, ActiveAdmin for this upload. But from my point of view, there is something wrong with post or something? Or the Params variable?


Solution

  • # app/views/activeadmin/dashboard/_import_cves.html.erb
    <%= form_tag admin_dashboard_import_cves_path, method: :post, multipart: true do %>
      <%= file_field_tag 'files[]', multiple: true %>
      <%= submit_tag 'Import CVEs' %>
    <% end %>
    

    ... worked (added [] to the file_field_tag). I just can't say how disappointed I'm that this wasn't anywhere in the documentation. Found it here: Rails file_field_tag with multiple files delivers random string to controller