Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 352
thurmc Avatar asked Mar 19 '23 21:03

thurmc


1 Answers

: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.

like image 198
Dylan Markow Avatar answered Mar 24 '23 13:03

Dylan Markow