Routing Question

Conrad Taylor wrote in post #1019488:

Sent from my iPhone

be something like the following:

match '/:id' => 'users#show', :constraints => { :id => /^[1-9]\d*/ }

The above can easily be fixed by adding a $ after the *. For example,

/^[1-9]\d*$/

Why do you continue to claim that you can use anchors in a constraint? Do you even know what an anchor is?

The x after the final / isn't needed being that the begin and end tokens take care of that for you.

Apparently, you don't know what regex flags do either.

Also, you can do all this in the context of a routes.rb using the constrains option to match.

No. You cannot.

Bruno Meira wrote in post #1019487:

Hi 7stud, I wanted a URL like that: /users/:id, where :id could only be one or more integers, actually the user id.

What is it that you find lacking in the normal rails routing, e.g.

resources :users

that requires you to change it?

Conrad Taylor wrote in post #1019488:

Sent from my iPhone

be

something like the following:

match ‘/:id’ => ‘users#show’, :constraints => { :id => /[1]\d*/ }

The above can easily be fixed by adding a $ after the *. For example,

/[2]\d*$/

Why do you continue to claim that you can use anchors in a constraint?

In regards to Rails routing, the start anchor ^ is implied as stated in section

3.8 of the routing documentation.

Do you even know what an anchor is?

The x after the final / isn’t needed being that the begin and end tokens

take care of that for you.

Apparently, you don’t know what regex flags do either.

You’re using the x flag because you have implemented your RegEx across several lines

where the spaces are not escaped. However, one could have easily written the same thing

in a single line and zero unescaped spaces. In short, this is just another way to do the same

thing.

-Conrad


  1. 1-9 ↩︎

  2. 1-9 ↩︎

Conrad Taylor wrote in post #1019592:

> > The above can easily be fixed by adding a $ after the *. For example, > > /^[1-9]\d*$/ >

Why do you continue to claim that you can use anchors in a constraint?

In regards to Rails routing, the start anchor ^ is implied as stated in section 3.8 of the routing documentation.

Nevertheless, you can't use anchors in the regex which you specify for a constraint, or you will get an error:

ArgumentError Regexp anchor characters are not allowed in routing requirements: /^[1-9]\d*/

Even though I tested the regex constraint before, I am now getting different results--they show that the regex has an implied anchor at both the beginning and at the end of the regex. For instance, if this is the only route in my routes.db file:

match "/users/:id" => "users#show",                         :constraints => {:id => /[1-9]\d*/}

...then the following urls match:

http://localhost:3000/users/1 http://localhost:3000/users/10

but these urls do not match:

http://localhost:3000/users/a1 http://localhost:3000/users/1a http://localhost:3000/users/aa http://localhost:3000/users/01

They produce a routing error:

  No route matches "/users/xx"

If there was no implied 'begining of string' anchor in the regex, then the url:

http://localhost:3000/users/a1

would match. And if there was no implied 'end of string' anchor in the regex, then the url:

http://localhost:3000/users/1a

would match.

With the following route being the only route in my routes.rb file:

  resources :users

...then all the following urls match:

http://localhost:3000/users/1 http://localhost:3000/users/a1 http://localhost:3000/users/1a http://localhost:3000/users/aa http://localhost:3000/users/01

Just about any characters will match in the :id position. So, Jim ruther Nill gave Bruno the right suggestion from the start.

Conrad Taylor wrote in post #1019592:

>> > The above can easily be fixed by adding a $ after the *. For example,

>> > /^[1-9]\d*$/

>> Why do you continue to claim that you can use anchors in a constraint?

> In regards to Rails routing, the start anchor ^ is implied as stated in > section > 3.8 of the routing documentation.

Nevertheless, you can't use anchors in the regex which you specify for a constraint, or you will get an error:

ArgumentError Regexp anchor characters are not allowed in routing requirements: /^[1-9]\d*/

Yes, this is correct and it's the current expected behavior.

Even though I tested the regex constraint before, I am now getting different results--they show that the regex has an implied anchor at both the beginning and at the end of the regex. For instance, if this is the only route in my routes.db file:

match "/users/:id" => "users#show", :constraints => {:id => /[1-9]\d*/}

...then the following urls match:

http://localhost:3000/users/1http://localhost:3000/users/10

but these urls do not match:

http://localhost:3000/users/a1http://localhost:3000/users/1ahttp://localhost:3000/users/aahttp://localhost:3000/users/01

They produce a routing error:

No route matches "/users/xx"

If there was no implied 'begining of string' anchor in the regex, then the url:

http://localhost:3000/users/a1

would match. And if there was no implied 'end of string' anchor in the regex, then the url:

http://localhost:3000/users/1a

would match.

With the following route being the only route in my routes.rb file:

resources :users

...then all the following urls match:

http://localhost:3000/users/1http://localhost:3000/users/a1http://localhost:3000/users/1ahttp://localhost:3000/users/aahttp://localhost:3000/users/01

Just about any characters will match in the :id position. So, Jim ruther Nill gave Bruno the right suggestion from the start.

Yes, this is exactly what I instructed Bruno to do in my initial response to his question.

-Conrad

hmmmm, That's what I do to fix my routing. I guess that my question generated a little confusion ;/. Sorry for any incovenience

Thx all