Skip to content
This repository
Browse code

Document form_for behaviour when using file_field inside the block

  • Loading branch information...
commit b39dfd5936ef25b04d6caad3ceb048c68a79ea12 1 parent f1fdc4f
Joost Baaij authored September 21, 2010
4  actionpack/lib/action_view/helpers/form_helper.rb
@@ -668,11 +668,13 @@ def hidden_field(object_name, method, options = {})
668 668
         InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("hidden", options)
669 669
       end
670 670
 
671  
-      # Returns an file upload input tag tailored for accessing a specified attribute (identified by +method+) on an object
  671
+      # Returns a file upload input tag tailored for accessing a specified attribute (identified by +method+) on an object
672 672
       # assigned to the template (identified by +object+). Additional options on the input tag can be passed as a
673 673
       # hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example
674 674
       # shown.
675 675
       #
  676
+      # Using this method inside a +form_for+ block will set the enclosing form's encoding to <tt>multipart/form-data</tt>.
  677
+      #
676 678
       # ==== Examples
677 679
       #   file_field(:user, :avatar)
678 680
       #   # => <input type="file" id="user_avatar" name="user[avatar]" />
6  railties/guides/source/form_helpers.textile
Source Rendered
@@ -549,7 +549,7 @@ will produce the same output if the current year is 2009 and the value chosen by
549 549
 
550 550
 h3. Uploading Files
551 551
 
552  
-A common task is uploading some sort of file, whether it's a picture of a person or a CSV file containing data to process. The most important thing to remember with file uploads is that the form's encoding *MUST* be set to "multipart/form-data". If you forget to do this the file will not be uploaded. This can be done by passing +:multi_part => true+ as an HTML option. This means that in the case of +form_tag+ it must be passed in the second options hash and in the case of +form_for+ inside the +:html+ hash.
  552
+A common task is uploading some sort of file, whether it's a picture of a person or a CSV file containing data to process. The most important thing to remember with file uploads is that the rendered form's encoding *MUST* be set to "multipart/form-data". If you use +form_for+, this is done automatically. If you use +form_tag+, you must set it yourself, as per the following example.
553 553
 
554 554
 The following two forms both upload a file.
555 555
 
@@ -558,11 +558,13 @@ The following two forms both upload a file.
558 558
   <%= file_field_tag 'picture' %>
559 559
 <% end %>
560 560
 
561  
-<%= form_for @person, :html => {:multipart => true} do |f| %>
  561
+<%= form_for @person do |f| %>
562 562
   <%= f.file_field :picture %>
563 563
 <% end %>
564 564
 </erb>
565 565
 
  566
+NOTE: Since Rails 3.1, forms rendered using +form_for+ have their encoding set to <tt>multipart/form-data</tt> automatically once a +file_field+ is used inside the block. Previous versions required you to set this explicitly.
  567
+
566 568
 Rails provides the usual pair of helpers: the barebones +file_field_tag+ and the model oriented +file_field+. The only difference with other helpers is that you cannot set a default value for file inputs as this would have no meaning. As you would expect in the first case the uploaded file is in +params[:picture]+ and in the second case in +params[:person][:picture]+.
567 569
 
568 570
 h4. What Gets Uploaded

0 notes on commit b39dfd5

Please sign in to comment.
Something went wrong with that request. Please try again.