Fix issue with `button_to`'s `to_form_params`

`button_to` was throwing exception when invoked with `params` hash that
contains symbol and string keys. The reason for the exception was that
`to_form_params` was comparing the given symbol and string keys.

The issue is fixed by turning all keys to strings inside
`to_form_params` before comparing them.
This commit is contained in:
Georgi Georgiev 2017-12-02 20:45:10 +02:00
parent cd84d87164
commit 113d8a2ba3
3 changed files with 21 additions and 3 deletions

View File

@ -1,3 +1,14 @@
* Fix issue with `button_to`'s `to_form_params`
`button_to` was throwing exception when invoked with `params` hash that
contains symbol and string keys. The reason for the exception was that
`to_form_params` was comparing the given symbol and string keys.
The issue is fixed by turning all keys to strings inside
`to_form_params` before comparing them.
*Georgi Georgiev*
* Mark arrays of translations as trusted safe by using the `_html` suffix.
Example:

View File

@ -634,7 +634,7 @@ module ActionView
# suitable for use as the names and values of form input fields:
#
# to_form_params(name: 'David', nationality: 'Danish')
# # => [{name: :name, value: 'David'}, {name: 'nationality', value: 'Danish'}]
# # => [{name: 'name', value: 'David'}, {name: 'nationality', value: 'Danish'}]
#
# to_form_params(country: { name: 'Denmark' })
# # => [{name: 'country[name]', value: 'Denmark'}]
@ -666,7 +666,7 @@ module ActionView
params.push(*to_form_params(value, array_prefix))
end
else
params << { name: namespace, value: attribute.to_param }
params << { name: namespace.to_s, value: attribute.to_param }
end
params.sort_by { |pair| pair[:name] }

View File

@ -77,11 +77,18 @@ class UrlHelperTest < ActiveSupport::TestCase
def test_to_form_params_with_hash
assert_equal(
[{ name: :name, value: "David" }, { name: :nationality, value: "Danish" }],
[{ name: "name", value: "David" }, { name: "nationality", value: "Danish" }],
to_form_params(name: "David", nationality: "Danish")
)
end
def test_to_form_params_with_hash_having_symbol_and_string_keys
assert_equal(
[{ name: "name", value: "David" }, { name: "nationality", value: "Danish" }],
to_form_params("name" => "David", :nationality => "Danish")
)
end
def test_to_form_params_with_nested_hash
assert_equal(
[{ name: "country[name]", value: "Denmark" }],