Skip to content

Commit

Permalink
Consider AC::Parameters as Hash in url_for
Browse files Browse the repository at this point in the history
Ref: #42020

It's not uncommon to want to forward request parameters,
as such the `:params` paramter of `url_for` often receive
an AC::Parameters instance.
  • Loading branch information
byroot committed Jun 25, 2021
1 parent 6ae78e9 commit fcc3cd3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 6 additions & 4 deletions actionpack/lib/action_dispatch/routing/route_set.rb
Expand Up @@ -822,10 +822,12 @@ def url_for(options, route_name = nil, url_strategy = UNKNOWN, method_name = nil
path = route_with_params.path(method_name)
params = route_with_params.params

if options[:params].is_a?(Hash)
params.merge! options[:params]
elsif options.key? :params
params[:params] = options[:params]
if options.key? :params
if options[:params]&.respond_to?(:to_hash)
params.merge! options[:params]
else
params[:params] = options[:params]
end
end

options[:path] = path
Expand Down
9 changes: 9 additions & 0 deletions actionpack/test/controller/url_for_test.rb
Expand Up @@ -362,6 +362,15 @@ def test_params_option
assert_equal({ id: "1" }.to_query, params[1])
end

def test_params_option_strong_parameters
request_params = ActionController::Parameters.new({ domain: "foo", id: "1", other: "BAD" })
url = W.new.url_for(only_path: true, controller: "c", action: "a", params: request_params.permit(:domain, :id))
params = extract_params(url)
assert_equal("/c/a?domain=foo&id=1", url)
assert_equal({ domain: "foo" }.to_query, params[0])
assert_equal({ id: "1" }.to_query, params[1])
end

def test_non_hash_params_option
url = W.new.url_for(only_path: true, controller: "c", action: "a", params: "p")
params = extract_params(url)
Expand Down

0 comments on commit fcc3cd3

Please sign in to comment.