thurmc
thurmc

Reputation: 525

Rails file field being interpreted as String?

I am trying to provide a form field to be a file input on a rails site. My form is set up like the following

<%= form_tag({:action => 'submit_bulk_adjustment',:id => 'uploadForm', :multipart => true}, {:method => :post}) %>
<%= file_field_tag :file, class: "file-selector"  %> ></td>
<%= submit_tag "Submit" %>

There are a few other forms in the field but probably not relevant. I am trying to use the file from the form field in a method (shown below) and I get the error "undefined method `tempfile' for "0033982687_1406831016_BulkTest.csv":String". What am I doing wrong here? I see almost identical code working on another website.

post = params[:file]

if(post == nil)
    raise NoFilenameEnteredError
end

post_path = post.tempfile.to_path.to_s

Upvotes: 4

Views: 1338

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124419

:multipart => true should be part of the second options hash, not the first one (the first one is just for the URL -- I assume that when you submit this form, you're actually seeing "&multipart=true" in the address bar?). Also, as @Vasseurth mentioned, you need to put your form elements in a block connected to the form:

<%= form_tag({:action => 'submit_bulk_adjustment',:id => 'uploadForm'}, {:multipart => true, :method => :post}) do %>
  <%= file_field_tag :file, class: "file-selector"  %>
  <%= submit_tag "Submit" %>
<% end %>

Also, the default method for form_tag is POST, so there's no need to specify that.

Upvotes: 7

Related Questions