Ruby on rails form not working with multiple files

Well, I'm searching this for a couple of days and I really couldn't find a solution. I'm trying to send multiple files and store it locally(or maybe in the future in a s3 bucket) and save also to the db.

I noticed that I'm getting a string instead of the file itself!

I'm using rails 5.1 and ruby 3.2

Here is my code:

Controller:

    all_files = params[:files]
    all_files.each do |fil|
        File.open(Rails.root.join('public', 'uploads', all_files.original_filename), 'wb') do |filea|
            filea.write(all_files.read)
        end
    end

form

<%= form_for @docs, :url => docs_create_path, :html => { :multipart => true } do |f| %>
<%= f.file_field :files, :multiple => 'multiple', :name => 'files[]'%>
<%= f.submit( "Upload file" ) %> <% end %>

Common errors: undefined method `original_filename' for #Array:0x0000000006aeb338>

Upvotes: 0

Views: 569

Answers (1)

Anand
Anand

Reputation: 6531

there is small mistake here replace all_files.orginal with file.orginal

all_files = params[:files]
all_files.each do |file|
    File.open(Rails.root.join('public', 'uploads', file.original_filename), 'wb') do |temp_file|
        temp_file.write(temp_file.read)
    end
end

Upvotes: 1

Related Questions