Skip to content
This repository
Browse code

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
  • Loading branch information...
commit 44a9aedd7b8d65517b15bbbb7729f3f16991e23f 1 parent fe58853
Ryan McGeary rmm5t authored
7 actionpack/CHANGELOG.md
Source Rendered
... ... @@ -1,5 +1,12 @@
1 1 ## unreleased ##
2 2
  3 +* Fix explicit names on multiple file fields. If a file field tag is passed
  4 + the multiple option, it is turned into an array field (appending `[]`),
  5 + but if the file field is passed an explicit name as an option, leave the
  6 + name alone (do not append `[]`). Fixes #9830
  7 +
  8 + *Ryan McGeary*
  9 +
3 10 * Fix assets loading performance in 3.2.13.
4 11
5 12 Issue #8756 uses Sprockets for resolving files that already exist on disk,
15 actionpack/lib/action_view/helpers/form_helper.rb
@@ -1196,27 +1196,26 @@ def add_default_name_and_id_for_value(tag_value, options)
1196 1196
1197 1197 def add_default_name_and_id(options)
1198 1198 if options.has_key?("index")
1199   - options["name"] ||= tag_name_with_index(options["index"])
  1199 + options["name"] ||= tag_name_with_index(options["index"], options["multiple"])
1200 1200 options["id"] = options.fetch("id"){ tag_id_with_index(options["index"]) }
1201 1201 options.delete("index")
1202 1202 elsif defined?(@auto_index)
1203   - options["name"] ||= tag_name_with_index(@auto_index)
  1203 + options["name"] ||= tag_name_with_index(@auto_index, options["multiple"])
1204 1204 options["id"] = options.fetch("id"){ tag_id_with_index(@auto_index) }
1205 1205 else
1206   - options["name"] ||= tag_name
  1206 + options["name"] ||= tag_name(options["multiple"])
1207 1207 options["id"] = options.fetch("id"){ tag_id }
1208 1208 end
1209 1209
1210   - options["name"] += "[]" if options["multiple"] && !options["name"].ends_with?("[]")
1211 1210 options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence
1212 1211 end
1213 1212
1214   - def tag_name
1215   - "#{@object_name}[#{sanitized_method_name}]"
  1213 + def tag_name(multiple = false)
  1214 + "#{@object_name}[#{sanitized_method_name}]#{"[]" if multiple}"
1216 1215 end
1217 1216
1218   - def tag_name_with_index(index)
1219   - "#{@object_name}[#{index}][#{sanitized_method_name}]"
  1217 + def tag_name_with_index(index, multiple = false)
  1218 + "#{@object_name}[#{index}][#{sanitized_method_name}]#{"[]" if multiple}"
1220 1219 end
1221 1220
1222 1221 def tag_id
10 actionpack/test/template/form_helper_test.rb
@@ -301,6 +301,16 @@ def test_file_field_has_no_size
301 301 assert_dom_equal expected, file_field("user", "avatar")
302 302 end
303 303
  304 + def test_file_field_with_multiple_behavior
  305 + expected = '<input id="import_file" multiple="multiple" name="import[file][]" type="file" />'
  306 + assert_dom_equal expected, file_field("import", "file", :multiple => true)
  307 + end
  308 +
  309 + def test_file_field_with_multiple_behavior_and_explicit_name
  310 + expected = '<input id="import_file" multiple="multiple" name="custom" type="file" />'
  311 + assert_dom_equal expected, file_field("import", "file", :multiple => true, :name => "custom")
  312 + end
  313 +
304 314 def test_hidden_field
305 315 assert_dom_equal '<input id="post_title" name="post[title]" type="hidden" value="Hello World" />',
306 316 hidden_field("post", "title")

0 comments on commit 44a9aed

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