diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-01-02 13:54:26 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-01-02 13:57:33 -0300 |
commit | c455817804e4df64c46c17a0cdec0e5a1ca5ba2e (patch) | |
tree | fc567eb385b14ebc8ca92bdc6972026bf6c586cd /actionview/lib | |
parent | 8b3cd74b8a09ef85a43d7631bb062a9ec7f57227 (diff) | |
parent | 00b26532f05283d2b160308522d1bd2146d6ac18 (diff) | |
download | rails-c455817804e4df64c46c17a0cdec0e5a1ca5ba2e.tar.gz rails-c455817804e4df64c46c17a0cdec0e5a1ca5ba2e.tar.bz2 rails-c455817804e4df64c46c17a0cdec0e5a1ca5ba2e.zip |
Merge pull request #17973 from maurogeorge/file_field_hidden_field
Generate a hidden_tag when using a file_field
Diffstat (limited to 'actionview/lib')
-rw-r--r-- | actionview/lib/action_view/helpers/form_helper.rb | 17 | ||||
-rw-r--r-- | actionview/lib/action_view/helpers/tags/file_field.rb | 15 |
2 files changed, 32 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 diff --git a/actionview/lib/action_view/helpers/tags/file_field.rb b/actionview/lib/action_view/helpers/tags/file_field.rb index 476b820d84..e6a1d9c62d 100644 --- a/actionview/lib/action_view/helpers/tags/file_field.rb +++ b/actionview/lib/action_view/helpers/tags/file_field.rb @@ -2,6 +2,21 @@ module ActionView module Helpers module Tags # :nodoc: class FileField < TextField # :nodoc: + + def render + options = @options.stringify_keys + + if options.fetch("include_hidden", true) + add_default_name_and_id(options) + options[:type] = "file" + tag("input", name: options["name"], type: "hidden", value: "") + tag("input", options) + else + options.delete("include_hidden") + @options = options + + super + end + end end end end |