Ruby on Rails Thursday, September 1, 2011

Conrad Taylor wrote in post #1019592:
> On Thu, Sep 1, 2011 at 1:53 AM, 7stud -- <lists@ruby-forum.com> wrote:
>
>> >
>> > 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.

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment