i have in my application a custom route, that need to match everytime the param is a number or an list of numbers splited by |
this is my code:
get '/lists' => 'cidades#list'
get '/list(/:id)' => 'cidades#list', :constraints => { :id => /[0-9|]+/ }
get '/list(/:name)' => 'cidades#list'
what i hope to accomplish is something like this:
/lists => cidades#list
/list/1 => cidades#list & param[:id] = "1"
/list/1|2|3|4 => cidades#list & param[:id] = "1|2|3|4"
/list/1a => cidades#list & param[:name] = "1a"
if i use this regexp: /[0-9|]+/
i get 1a
to be valid, but i don't want this. I find this regexp: /\A[0-9|]+\Z/
but this gives me this error:
ArgumentError (Regexp anchor characters are not allowed in routing requirements: /\A[0-9|]+\Z/):
how can i create a constraint that will only match a numeric string? ( a string with only numbers )
By default rails anchors the routing regular expressions so there is no need for the \A
and \Z
. I think you're looking for something that follows this pattern:
:constraints => { :id => /[0-9]+(\%7C[0-9]+)*/ }
This will force the route to have at least one integer (0-9). Optionally, the route can be followed by a | character (which is converted to %7C
by the browser). The | character is then followed by an additional integer (0-9). Putting this patter in its own group prevents having any lingering | after the integer ids.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With