Skip to content

Commit

Permalink
when calling url_for with a hash, additional (likely unwanted) values…
Browse files Browse the repository at this point in the history
… (such as :host) would be returned in the hash... calling #dup on the hash prevents this
  • Loading branch information
akaspick committed Sep 8, 2011
1 parent de178df commit 45b7731
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion actionpack/lib/action_dispatch/routing/url_for.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def url_for(options = nil)
when String
options
when nil, Hash
_routes.url_for((options || {}).reverse_merge!(url_options).symbolize_keys)
_routes.url_for((options.dup || {}).reverse_merge!(url_options).symbolize_keys)
else
polymorphic_url(options)
end
Expand Down
12 changes: 12 additions & 0 deletions actionpack/test/dispatch/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,18 @@ def test_local
end
end

# tests the use of dup in url_for
def test_url_for_with_no_side_effects
# without dup, additional (and possibly unwanted) values will be present in the options (eg. :host)
original_options = {:controller => 'projects', :action => 'status'}
options = original_options.dup

url_for options

# verify that the options passed in have not changed from the original ones
assert_equal original_options, options
end

def test_projects_status
with_test_routes do
assert_equal '/projects/status', url_for(:controller => 'projects', :action => 'status', :only_path => true)
Expand Down

0 comments on commit 45b7731

Please sign in to comment.