aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/lib/action_view/helpers/form_helper.rb
diff options
context:
space:
mode:
authorMauro George <maurogot@gmail.com>2014-12-08 20:22:36 -0200
committerMauro George <maurogot@gmail.com>2015-01-02 12:15:03 -0200
commit00b26532f05283d2b160308522d1bd2146d6ac18 (patch)
tree593b939899618f5fecb8134ce4ee959e835adb0d /actionview/lib/action_view/helpers/form_helper.rb
parent6442c90f098f7095ada0cd66bb7291b6798a5583 (diff)
downloadrails-00b26532f05283d2b160308522d1bd2146d6ac18.tar.gz
rails-00b26532f05283d2b160308522d1bd2146d6ac18.tar.bz2
rails-00b26532f05283d2b160308522d1bd2146d6ac18.zip
Add a hidden_field on the file_field
This will avoid a error be raised when the only input on the form is the `file_field`.
Diffstat (limited to 'actionview/lib/action_view/helpers/form_helper.rb')
-rw-r--r--actionview/lib/action_view/helpers/form_helper.rb17
1 files changed, 17 insertions, 0 deletions
diff --git a/actionview/lib/action_view/helpers/form_helper.rb b/actionview/lib/action_view/helpers/form_helper.rb
index c4371dc705..e3a8f998a8 100644
--- a/actionview/lib/action_view/helpers/form_helper.rb
+++ b/actionview/lib/action_view/helpers/form_helper.rb
@@ -854,6 +854,23 @@ module ActionView
#
# file_field(:attachment, :file, class: 'file_input')
# # => <input type="file" id="attachment_file" name="attachment[file]" class="file_input" />
+ #
+ # ==== Gotcha
+ #
+ # The HTML specification says when nothing is select on a file field web browsers do not send any value to server.
+ # Unfortunately this introduces a gotcha:
+ # if an +User+ model has a +avatar+ field, and in the form none file is selected no +avatar+ parameter is sent. So,
+ # any mass-assignment idiom like
+ #
+ # @user.update(params[:user])
+ #
+ # wouldn't update avatar.
+ #
+ # To prevent this the helper generates an auxiliary hidden field before
+ # every file field. The hidden field has the same name as file field and blank value.
+ #
+ # In case if you don't want the helper to generate this hidden field you can specify
+ # <tt>include_hidden: false</tt> option.
def file_field(object_name, method, options = {})
Tags::FileField.new(object_name, method, self, options).render
end