aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuo Xiang Tan <tgx_world@hotmail.com>2014-11-22 21:42:19 +0800
committerGuo Xiang Tan <tgx_world@hotmail.com>2014-11-23 08:23:26 +0800
commit46041c520809714b3937e7c79c2f220018a4a111 (patch)
tree9a8d7b424765ac519f6b75e5f0fb2fe4c860ff10
parent555f95bd0621b8d2e666f6989927eee7d4b7f42d (diff)
downloadrails-46041c520809714b3937e7c79c2f220018a4a111.tar.gz
rails-46041c520809714b3937e7c79c2f220018a4a111.tar.bz2
rails-46041c520809714b3937e7c79c2f220018a4a111.zip
Anchor should not be appended when set to nil/false.
Fixes https://github.com/rails/rails/issues/17714.
-rw-r--r--actionpack/lib/action_dispatch/http/url.rb4
-rw-r--r--actionpack/test/controller/routing_test.rb3
-rw-r--r--actionpack/test/controller/url_for_test.rb14
3 files changed, 20 insertions, 1 deletions
diff --git a/actionpack/lib/action_dispatch/http/url.rb b/actionpack/lib/action_dispatch/http/url.rb
index 6b8dcaf497..22c0de2ac2 100644
--- a/actionpack/lib/action_dispatch/http/url.rb
+++ b/actionpack/lib/action_dispatch/http/url.rb
@@ -68,7 +68,9 @@ module ActionDispatch
end
def add_anchor(path, anchor)
- path << "##{Journey::Router::Utils.escape_fragment(anchor.to_param.to_s)}"
+ if anchor
+ path << "##{Journey::Router::Utils.escape_fragment(anchor.to_param)}"
+ end
end
def extract_domain_from(host, tld_length)
diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb
index c18914cc8e..aca9f03748 100644
--- a/actionpack/test/controller/routing_test.rb
+++ b/actionpack/test/controller/routing_test.rb
@@ -1001,6 +1001,9 @@ class RouteSetTest < ActiveSupport::TestCase
assert_equal "http://test.host/people?baz=bar#location",
controller.send(:index_url, :baz => "bar", :anchor => 'location')
+
+ assert_equal "http://test.host/people", controller.send(:index_url, anchor: nil)
+ assert_equal "http://test.host/people", controller.send(:index_url, anchor: false)
end
def test_named_route_url_method_with_port
diff --git a/actionpack/test/controller/url_for_test.rb b/actionpack/test/controller/url_for_test.rb
index c05cde87e4..f0eba17556 100644
--- a/actionpack/test/controller/url_for_test.rb
+++ b/actionpack/test/controller/url_for_test.rb
@@ -54,6 +54,20 @@ module AbstractController
)
end
+ def test_nil_anchor
+ assert_equal(
+ '/c/a',
+ W.new.url_for(only_path: true, controller: 'c', action: 'a', anchor: nil)
+ )
+ end
+
+ def test_false_anchor
+ assert_equal(
+ '/c/a',
+ W.new.url_for(only_path: true, controller: 'c', action: 'a', anchor: false)
+ )
+ end
+
def test_anchor_should_call_to_param
assert_equal('/c/a#anchor',
W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :anchor => Struct.new(:to_param).new('anchor'))