Skip to content
Browse files

do not append a second slash when using

  • Loading branch information...
1 parent bdaafae commit 0b2ce7d955041107c70998c0d5b354404b83c74b @senny senny committed Jan 2, 2013
Showing with 33 additions and 7 deletions.
  1. +12 −0 actionpack/CHANGELOG.md
  2. +5 −1 actionpack/lib/action_dispatch/http/url.rb
  3. +16 −6 actionpack/test/controller/routing_test.rb
View
12 actionpack/CHANGELOG.md
@@ -1,5 +1,17 @@
## Rails 4.0.0 (unreleased) ##
+* Do not append second slash to root_url when using `trailing_slash: true`
+ Fix #8700
+
+ Example:
+ # before
+ root_url # => http://test.host//
+
+ # after
+ root_url # => http://test.host/
+
+ *Yves Senn*
+
* Allow to toggle dumps on error pages.
*Gosha Arinich*
View
6 actionpack/lib/action_dispatch/http/url.rb
@@ -32,7 +32,11 @@ def url_for(options = {})
params.reject! { |_,v| v.to_param.nil? }
result = build_host_url(options)
- result << (options[:trailing_slash] ? path.sub(/\?|\z/) { "/" + $& } : path)
+ if options[:trailing_slash] && !path.ends_with?('/')
+ result << path.sub(/(\?|\z)/) { "/" + $& }
+ else
+ result << path
+ end
result << "?#{params.to_query}" unless params.empty?
result << "##{Journey::Router::Utils.escape_fragment(options[:anchor].to_param.to_s)}" if options[:anchor]
result
View
22 actionpack/test/controller/routing_test.rb
@@ -57,13 +57,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_options
- options = super
+ define_method :url_options do
+ options = super()
options[:protocol] ||= "http"
options[:host] ||= "test.host"
- options
+ options.merge(additional_options)
end
include helpers
@@ -428,8 +428,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
@@ -456,6 +456,16 @@ def test_named_route_root_without_hash
assert_equal("/", routes.send(:root_path))
end
+ def test_named_route_root_with_trailing_slash
+ rs.draw do
+ root "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
get 'page/:year/:month/:day/:title' => 'page#show', :as => 'article',

0 comments on commit 0b2ce7d

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