Reputation: 1399
We're running Rails 3.2.19 and Ruby 2.1.2.
We have a legacy line of code (actually, more than one, but this line is of particular concern) that reads
form_for [@commentable, @comment], :html => { :multipart => true, :class => "lightbox_form"} do |f|
The form data may or may not include an uploaded file at the user's discretion.
Many examples in SO refer to specifying the :multipart
setting, but then I see this answer at Form_for with :multipart => true spits out and then in looking at the docs at http://guides.rubyonrails.org/v3.2.19/form_helpers.html#uploading-files, I see that it shouldn't be necessary (except if using form_tag
). I also see this discussion at https://github.com/rails/rails/issues/10176 that adds to my confusion.
I'm asking in part because we're getting EOF errors in Rack (no content coming through on the multipart parser; see Rack throwing EOFError (bad content body) if you're interested in those details).
Our code may have been previously run under earlier versions of Rails when it may have been necessary (and so may just be a holdover). But given all the other examples on SO that include :multipart
, I want to better understand if or when :multipart
is needed with form_for
before I remove it and what side effects I may encounter.
Upvotes: 2
Views: 4371
Reputation: 2557
It's not required in Rails 3.1 and later, as long as you're using form_for
with file_field
like this:
<%= form_for @person do |f| %>
<%= f.file_field :picture %>
<% end %>
This will not work:
<%= form_for @person do |f| %>
<%= file_field_tag 'person[picture]' %>
<% end %>
You can easily verify that it's working. Inspect the generated HTML and look for the enctype="multipart/form-data"
attribute on the form tag. Rails doesn't do any magic beyond setting the encoding type, so if the attribute is there, you're good.
Upvotes: 7