From 4aae8bfe4dc6da1722e7f0f1722cf1f7ef472c19 Mon Sep 17 00:00:00 2001 From: Jon Moss Date: Mon, 17 Oct 2016 21:16:54 -0400 Subject: Convert ActionController::Parameters to a hash in button_to MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before, an error would be raised saying that the method `to_param` was undefined on the instance of `ActionController::Parameters`. Now, we are checking to see if the `params` object being passed to `button_to` responds to the `permitted?` method, and if so, we will call `to_h` on it. If it does not respond to `permitted?`, then the `params` will remain unchanged. [Jon Moss, Rafael Mendonça França] --- actionview/lib/action_view/helpers/url_helper.rb | 6 ++++++ actionview/test/template/url_helper_test.rb | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) (limited to 'actionview') diff --git a/actionview/lib/action_view/helpers/url_helper.rb b/actionview/lib/action_view/helpers/url_helper.rb index dad0e9dac3..2445887c17 100644 --- a/actionview/lib/action_view/helpers/url_helper.rb +++ b/actionview/lib/action_view/helpers/url_helper.rb @@ -331,6 +331,12 @@ module ActionView inner_tags = method_tag.safe_concat(button).safe_concat(request_token_tag) if params + params = if params.respond_to?(:permitted?) + params.to_h + else + params + end + to_form_params(params).each do |param| inner_tags.safe_concat tag(:input, type: "hidden", name: param[:name], value: param[:value]) end diff --git a/actionview/test/template/url_helper_test.rb b/actionview/test/template/url_helper_test.rb index 2ef2be65d2..4f73241f52 100644 --- a/actionview/test/template/url_helper_test.rb +++ b/actionview/test/template/url_helper_test.rb @@ -221,6 +221,22 @@ class UrlHelperTest < ActiveSupport::TestCase ) end + class FakeParams + def permitted? + end + + def to_h + { foo: :bar, baz: "quux" } + end + end + + def test_button_to_with_strong_params + assert_dom_equal( + %{
}, + button_to("Hello", "http://www.example.com", params: FakeParams.new) + ) + end + def test_button_to_with_nested_hash_params assert_dom_equal( %{
}, -- cgit v1.2.3