diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-04-05 12:53:26 -0700 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-04-05 12:53:26 -0700 |
commit | dbe25910faabedfeae3e1f284cbfb6af746201c6 (patch) | |
tree | 21873c3e66e6b40a4855e510b47c0df1f1a965d3 | |
parent | fe588537654adc583d54157b1a5db48786452adf (diff) | |
parent | 44a9aedd7b8d65517b15bbbb7729f3f16991e23f (diff) | |
download | rails-dbe25910faabedfeae3e1f284cbfb6af746201c6.tar.gz rails-dbe25910faabedfeae3e1f284cbfb6af746201c6.tar.bz2 rails-dbe25910faabedfeae3e1f284cbfb6af746201c6.zip |
Merge pull request #10105 from rmm5t/fix-explicit-name-on-multiple-fields
Fix explicit names on multiple file fields
-rw-r--r-- | actionpack/CHANGELOG.md | 7 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/form_helper.rb | 15 | ||||
-rw-r--r-- | actionpack/test/template/form_helper_test.rb | 10 |
3 files changed, 24 insertions, 8 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 89029af3e9..5c9bcc7545 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,5 +1,12 @@ ## unreleased ## +* Fix explicit names on multiple file fields. If a file field tag is passed + the multiple option, it is turned into an array field (appending `[]`), + but if the file field is passed an explicit name as an option, leave the + name alone (do not append `[]`). Fixes #9830 + + *Ryan McGeary* + * Fix assets loading performance in 3.2.13. Issue #8756 uses Sprockets for resolving files that already exist on disk, diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 920dc3f794..0c079256a3 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -1196,27 +1196,26 @@ module ActionView def add_default_name_and_id(options) if options.has_key?("index") - options["name"] ||= tag_name_with_index(options["index"]) + options["name"] ||= tag_name_with_index(options["index"], options["multiple"]) options["id"] = options.fetch("id"){ tag_id_with_index(options["index"]) } options.delete("index") elsif defined?(@auto_index) - options["name"] ||= tag_name_with_index(@auto_index) + options["name"] ||= tag_name_with_index(@auto_index, options["multiple"]) options["id"] = options.fetch("id"){ tag_id_with_index(@auto_index) } else - options["name"] ||= tag_name + options["name"] ||= tag_name(options["multiple"]) options["id"] = options.fetch("id"){ tag_id } end - options["name"] += "[]" if options["multiple"] && !options["name"].ends_with?("[]") options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence end - def tag_name - "#{@object_name}[#{sanitized_method_name}]" + def tag_name(multiple = false) + "#{@object_name}[#{sanitized_method_name}]#{"[]" if multiple}" end - def tag_name_with_index(index) - "#{@object_name}[#{index}][#{sanitized_method_name}]" + def tag_name_with_index(index, multiple = false) + "#{@object_name}[#{index}][#{sanitized_method_name}]#{"[]" if multiple}" end def tag_id diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 7b35424ec7..22af39add4 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -301,6 +301,16 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, file_field("user", "avatar") end + def test_file_field_with_multiple_behavior + expected = '<input id="import_file" multiple="multiple" name="import[file][]" type="file" />' + assert_dom_equal expected, file_field("import", "file", :multiple => true) + end + + def test_file_field_with_multiple_behavior_and_explicit_name + expected = '<input id="import_file" multiple="multiple" name="custom" type="file" />' + assert_dom_equal expected, file_field("import", "file", :multiple => true, :name => "custom") + end + def test_hidden_field assert_dom_equal '<input id="post_title" name="post[title]" type="hidden" value="Hello World" />', hidden_field("post", "title") |