Skip to content
Browse files

Merge pull request #8703 from senny/backport_8700

Backport #8701, do not append a second slash with `trailing_slash: true`

Closes #8700
  • Loading branch information...
2 parents f4dc7e3 + 33841a9 commit f8452e8de786377ed89d777dabe7d82987f9b001 @rafaelfranca rafaelfranca committed Jan 2, 2013
Showing with 33 additions and 6 deletions.
  1. +13 −0 actionpack/CHANGELOG.md
  2. +5 −1 actionpack/lib/action_dispatch/http/url.rb
  3. +15 −5 actionpack/test/controller/routing_test.rb
View
13 actionpack/CHANGELOG.md
@@ -1,5 +1,18 @@
## Rails 3.2.11 (unreleased) ##
+* Do not append second slash to root_url when using `trailing_slash: true`
+ Fix #8700.
+ Backport #8701.
+
+ Example:
+ # before
+ root_url # => http://test.host//
+
+ # after
+ root_url # => http://test.host/
+
+ *Yves Senn*
+
* Fix a bug in `content_tag_for` that prevents it for work without a block.
*Jasl*
View
6 actionpack/lib/action_dispatch/http/url.rb
@@ -43,7 +43,11 @@ def url_for(options = {})
params = options[:params] || {}
params.reject! {|k,v| v.to_param.nil? }
- rewritten_url << (options[:trailing_slash] ? path.sub(/\?|\z/) { "/" + $& } : path)
+ if options[:trailing_slash] && !path.ends_with?('/')
+ rewritten_url << path.sub(/(\?|\z)/) { "/" + $& }
+ else
+ rewritten_url << path
+ end
rewritten_url << "?#{params.to_query}" unless params.empty?
rewritten_url << "##{Journey::Router::Utils.escape_fragment(options[:anchor].to_param.to_s)}" if options[:anchor]
rewritten_url
View
20 actionpack/test/controller/routing_test.rb
@@ -58,13 +58,13 @@ def test_route_generation_allows_passing_non_string_values_to_generated_helper
end
class MockController
- def self.build(helpers)
+ def self.build(helpers, additional_options = {})
Class.new do
- def url_for(options)
+ define_method :url_for do |options|
options[:protocol] ||= "http"
options[:host] ||= "test.host"
- super(options)
+ super(options.merge(additional_options))
end
include helpers
@@ -468,8 +468,8 @@ def test_optimised_named_route_with_host
routes.send(:pages_url)
end
- def setup_for_named_route
- MockController.build(rs.url_helpers).new
+ def setup_for_named_route(options = {})
+ MockController.build(rs.url_helpers, options).new
end
def test_named_route_without_hash
@@ -487,6 +487,16 @@ def test_named_route_root
assert_equal("/", routes.send(:root_path))
end
+ def test_named_route_root_with_trailing_slash
+ rs.draw do
+ root :to => "hello#index"
+ end
+
+ routes = setup_for_named_route(trailing_slash: true)
+ assert_equal("http://test.host/", routes.send(:root_url))
+ assert_equal("http://test.host/?foo=bar", routes.send(:root_url, foo: :bar))
+ end
+
def test_named_route_with_regexps
rs.draw do
match 'page/:year/:month/:day/:title' => 'page#show', :as => 'article',

0 comments on commit f8452e8

Please sign in to comment.
Something went wrong with that request. Please try again.