Skip to content

Commit 5df23a0

Browse files
authored
Merge pull request rails#31320 from gtqnchev/to_form_params-fix
Fix issue with `button_to`'s `to_form_params`
2 parents 3c8c410 + 113d8a2 commit 5df23a0

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

actionview/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
* Fix issue with `button_to`'s `to_form_params`
2+
3+
`button_to` was throwing exception when invoked with `params` hash that
4+
contains symbol and string keys. The reason for the exception was that
5+
`to_form_params` was comparing the given symbol and string keys.
6+
7+
The issue is fixed by turning all keys to strings inside
8+
`to_form_params` before comparing them.
9+
10+
*Georgi Georgiev*
11+
112
* Mark arrays of translations as trusted safe by using the `_html` suffix.
213

314
Example:

actionview/lib/action_view/helpers/url_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ def method_tag(method)
634634
# suitable for use as the names and values of form input fields:
635635
#
636636
# to_form_params(name: 'David', nationality: 'Danish')
637-
# # => [{name: :name, value: 'David'}, {name: 'nationality', value: 'Danish'}]
637+
# # => [{name: 'name', value: 'David'}, {name: 'nationality', value: 'Danish'}]
638638
#
639639
# to_form_params(country: { name: 'Denmark' })
640640
# # => [{name: 'country[name]', value: 'Denmark'}]
@@ -666,7 +666,7 @@ def to_form_params(attribute, namespace = nil)
666666
params.push(*to_form_params(value, array_prefix))
667667
end
668668
else
669-
params << { name: namespace, value: attribute.to_param }
669+
params << { name: namespace.to_s, value: attribute.to_param }
670670
end
671671

672672
params.sort_by { |pair| pair[:name] }

actionview/test/template/url_helper_test.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,18 @@ def test_url_for_with_invalid_referer
7777

7878
def test_to_form_params_with_hash
7979
assert_equal(
80-
[{ name: :name, value: "David" }, { name: :nationality, value: "Danish" }],
80+
[{ name: "name", value: "David" }, { name: "nationality", value: "Danish" }],
8181
to_form_params(name: "David", nationality: "Danish")
8282
)
8383
end
8484

85+
def test_to_form_params_with_hash_having_symbol_and_string_keys
86+
assert_equal(
87+
[{ name: "name", value: "David" }, { name: "nationality", value: "Danish" }],
88+
to_form_params("name" => "David", :nationality => "Danish")
89+
)
90+
end
91+
8592
def test_to_form_params_with_nested_hash
8693
assert_equal(
8794
[{ name: "country[name]", value: "Denmark" }],

0 commit comments

Comments
 (0)