Skip to content
This repository
Browse code

Add support for optional root segments containing slashes

Optional segments with a root scope need to have the leading slash
outside of the parentheses, otherwise the generated url will be empty.
However if the route has non-optional elements then the leading slash
needs to remain inside the parentheses otherwise the generated url
will have two leading slashes, e.g:

Blog::Application.routes.draw do
  get '/(:category)', :to => 'posts#index', :as => :root
  get '/(:category)/author/:name', :to => 'posts#author', :as => :author
end

$ rake routes
  root GET /(:category)(.:format)              posts#index
author GET (/:category)/author/:name(.:format) posts#author

This change adds support for optional segments that contain a slash,
allowing support for urls like /page/2 for the root path, e.g:

Blog::Application.routes.draw do
  get '/(page/:page)', :to => 'posts#index', :as => :root
end

$ rake routes
root GET /(page/:page)(.:format) posts#index

Fixes #7073
  • Loading branch information...
commit d8745decaf59aad32aa2f09abdba99b8d0e48b31 1 parent 939f014
Andrew White authored July 17, 2012
2  actionpack/lib/action_dispatch/routing/mapper.rb
@@ -262,7 +262,7 @@ def defaults_from_constraints(constraints)
262 262
       # for root cases, where the latter is the correct one.
263 263
       def self.normalize_path(path)
264 264
         path = Journey::Router::Utils.normalize_path(path)
265  
-        path.gsub!(%r{/(\(+)/?}, '\1/') unless path =~ %r{^/\(+[^/]+\)$}
  265
+        path.gsub!(%r{/(\(+)/?}, '\1/') unless path =~ %r{^/\(+[^)]+\)$}
266 266
         path
267 267
       end
268 268
 
27  actionpack/test/dispatch/routing_test.rb
@@ -2679,3 +2679,30 @@ def show
2679 2679
     end
2680 2680
   end
2681 2681
 end
  2682
+
  2683
+class TestOptionalRootSegments < ActionDispatch::IntegrationTest
  2684
+  stub_controllers do |routes|
  2685
+    Routes = routes
  2686
+    Routes.draw do
  2687
+      get '/(page/:page)', :to => 'pages#index', :as => :root
  2688
+    end
  2689
+  end
  2690
+
  2691
+  def app
  2692
+    Routes
  2693
+  end
  2694
+
  2695
+  include Routes.url_helpers
  2696
+
  2697
+  def test_optional_root_segments
  2698
+    get '/'
  2699
+    assert_equal 'pages#index', @response.body
  2700
+    assert_equal '/', root_path
  2701
+
  2702
+    get '/page/1'
  2703
+    assert_equal 'pages#index', @response.body
  2704
+    assert_equal '1', @request.params[:page]
  2705
+    assert_equal '/page/1', root_path('1')
  2706
+    assert_equal '/page/1', root_path(:page => '1')
  2707
+  end
  2708
+end

0 notes on commit d8745de

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