diff options
author | Trevor Turk <trevorturk@gmail.com> | 2010-06-25 18:50:56 -0500 |
---|---|---|
committer | Trevor Turk <trevorturk@gmail.com> | 2010-06-25 18:50:56 -0500 |
commit | 81e9627c7f4bad6007a5f8b5b7f5d50c9b28969c (patch) | |
tree | 9ad7521bb589ab322a378a7f04e302c1c53d715e | |
parent | dd5924d8ca06334898831ffede11efe2c264b5f0 (diff) | |
download | rails-81e9627c7f4bad6007a5f8b5b7f5d50c9b28969c.tar.gz rails-81e9627c7f4bad6007a5f8b5b7f5d50c9b28969c.tar.bz2 rails-81e9627c7f4bad6007a5f8b5b7f5d50c9b28969c.zip |
Provide example for working around that regexp anchors can't be in route constraints
-rw-r--r-- | railties/guides/source/routing.textile | 11 |
1 files changed, 10 insertions, 1 deletions
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: <ruby> -match '/:id' => 'videos#show', :constraints => {:id => /^\d/} +match '/:id' => 'posts#show', :constraints => {:id => /^\d/} +</ruby> + +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: + +<ruby> +match '/:id' => 'posts#show', :constraints => { :id => /\d.+/ } +match '/:username' => 'users#show' </ruby> h4. Request-Based Constraints |