From 81e9627c7f4bad6007a5f8b5b7f5d50c9b28969c Mon Sep 17 00:00:00 2001 From: Trevor Turk Date: Fri, 25 Jun 2010 18:50:56 -0500 Subject: Provide example for working around that regexp anchors can't be in route constraints --- railties/guides/source/routing.textile | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'railties/guides') diff --git a/railties/guides/source/routing.textile b/railties/guides/source/routing.textile index fe0e2f7d40..9ff06856c3 100644 --- a/railties/guides/source/routing.textile +++ b/railties/guides/source/routing.textile @@ -447,7 +447,16 @@ match 'photo/:id' => 'photos#show', :id => /[A-Z]\d{5}/ +:constraints+ takes regular expression. However note that regexp anchors can't be used within constraints. For example following route will not work: -match '/:id' => 'videos#show', :constraints => {:id => /^\d/} +match '/:id' => 'posts#show', :constraints => {:id => /^\d/} + + +However, note that you don't need to use anchors because all routes are anchored at the start. + +For example, the following routes would allow for +posts+ with +to_param+ values like +1-hello-world+ that always begin with a number and +users+ with +to_param+ values like +david+ that never begin with a number to share the root namespace: + + +match '/:id' => 'posts#show', :constraints => { :id => /\d.+/ } +match '/:username' => 'users#show' h4. Request-Based Constraints -- cgit v1.2.3