Joe Morano
Joe Morano

Reputation: 1895

Can't find submitted file in params

All I'm trying to do is parse a file with Rails, but I can't for the life of me pass it through to my controller. I can't even get the file name or path to show up in the submitted params.

My form:

<%= form_tag({url: upload_path}, method: :patch, multipart: true) do %>
  <%= file_field(:user, :csv, :multiple => false, class:"file-field") %>
  <%= submit_tag 'Submit', class:"btn" %>
<% end %>

My controller:

def upload
  file = params[:user][:csv] #params[:user] is nil
  #parse file
end

According to the rails docs, I thought the file should be contained in params[:user][:csv], but I must be misinterpreting something because params[:user][:csv] is nil, and neither "user" or "csv" show up anywhere in the params.

I want to be able to get the file path, and then using that, parse the file. What am I doing wrong?

Upvotes: 1

Views: 597

Answers (1)

Michael Malov
Michael Malov

Reputation: 1887

Use this for file_field_tag

<%= file_field_tag('user["csv"]', :multiple => false, class:"file-field") %>

Options should go after name and I don't understand what is :csv in that case if :user is name

Upvotes: 1

Related Questions