Validation on Routes

3 views
Skip to first unread message

Nikhil Goyal

unread,
Oct 31, 2011, 5:32:50 PM10/31/11
to Ruby on Rails: Talk
I have a route that is something like this


match '/:token' => 'users#new'


This token is 8 digit alphanumeric. I want to add validations on it so
that it matches with the route only when there are 8 digits present in
token.

How can I do that.

Everaldo Gomes

unread,
Oct 31, 2011, 5:57:10 PM10/31/11
to rubyonra...@googlegroups.com
Hi!

Try this:

match ':/:token' => 'users#new' , constraints => {  :token => /[a-zA-Z0-9]{8}/ }

There is \w in regexp that means letter, number and underscore (accordlying rubular.com) , but I don't know if your token accepts underscores.





--
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 rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


Nikhil Goyal

unread,
Oct 31, 2011, 7:36:39 PM10/31/11
to Ruby on Rails: Talk


On Nov 1, 2:57 am, Everaldo Gomes <everaldo.go...@gmail.com> wrote:
> Hi!
>
> Try this:
>
> match ':/:token' => 'users#new' , constraints => {  :token =>
> /[a-zA-Z0-9]{8}/ }
>
> There is \w in regexp that means letter, number and underscore (accordlying
> rubular.com) , but I don't know if your token accepts underscores.
>


No there are no underscores. only alphabets and numbers.

Everaldo Gomes

unread,
Oct 31, 2011, 8:03:19 PM10/31/11
to rubyonra...@googlegroups.com
So, try using /[a-zA-Z0-9]/ as your constraint. I can't remember a shortcut for alphanumeric, if there is one.
Reply all
Reply to author
Forward
0 new messages