aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides
diff options
context:
space:
mode:
authorJoost Baaij <joost@spacebabies.nl>2010-09-21 15:11:04 +0200
committerJoost Baaij <joost@spacebabies.nl>2010-09-21 15:11:04 +0200
commitb39dfd5936ef25b04d6caad3ceb048c68a79ea12 (patch)
treeb395c9ecfaed39382a73211a3a7c246d4d42ebf6 /railties/guides
parentf1fdc4ff0a88c4c906099982bbcb4c9a23b4be3f (diff)
downloadrails-b39dfd5936ef25b04d6caad3ceb048c68a79ea12.tar.gz
rails-b39dfd5936ef25b04d6caad3ceb048c68a79ea12.tar.bz2
rails-b39dfd5936ef25b04d6caad3ceb048c68a79ea12.zip
Document form_for behaviour when using file_field inside the block
Diffstat (limited to 'railties/guides')
-rw-r--r--railties/guides/source/form_helpers.textile6
1 files changed, 4 insertions, 2 deletions
diff --git a/railties/guides/source/form_helpers.textile b/railties/guides/source/form_helpers.textile
index 26cbdfc44f..e3b9d745b2 100644
--- a/railties/guides/source/form_helpers.textile
+++ b/railties/guides/source/form_helpers.textile
@@ -549,7 +549,7 @@ will produce the same output if the current year is 2009 and the value chosen by
h3. Uploading Files
-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.
+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.
The following two forms both upload a file.
@@ -558,11 +558,13 @@ The following two forms both upload a file.
<%= file_field_tag 'picture' %>
<% end %>
-<%= form_for @person, :html => {:multipart => true} do |f| %>
+<%= form_for @person do |f| %>
<%= f.file_field :picture %>
<% end %>
</erb>
+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.
+
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]+.
h4. What Gets Uploaded