Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with button_to's to_form_params #31320

Merged
merged 1 commit into from
Jul 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions actionview/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
4 changes: 2 additions & 2 deletions actionview/lib/action_view/helpers/url_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ def method_tag(method)
# 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'}]
Expand Down Expand Up @@ -666,7 +666,7 @@ def to_form_params(attribute, namespace = nil)
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] }
Expand Down
9 changes: 8 additions & 1 deletion actionview/test/template/url_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,18 @@ def test_url_for_with_invalid_referer

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" }],
Expand Down