Ghanshyam Anand
Ghanshyam Anand

Reputation: 174

Regex in Rails Routing

I want to create a routes where any value is accepted, I have created one like this

get '/coworking-in-:coworking_place', to: 'home#coworking', constraints: { :coworking_place => /[a-z]*[A-Z]*/ }

It is working fine with following cases:

  1. www.aboardoffices.com/coworking-in-delhi
  2. www.aboardoffices.com/coworking-in-mumbai

But it is not working in the following cases:

  1. www.aboardoffices.com/coworking-in-sector7Dwarka
  2. www.aboardoffices.com/coworking-in-dlfPhase1Gurgaon

I want to create route that can accept any parameters in :coworking_place

Upvotes: 0

Views: 774

Answers (1)

kiddorails
kiddorails

Reputation: 13014

Your regular expression /[a-z]*[A-Z]*/ doesn't capture all the groups. See - http://rubular.com/r/laRElzp1QU

You probably need /[a-zA-Z0-9-]+/. This whitelists all upper-case, lowercase characters, numbers, and -. Check result - http://rubular.com/r/niCKQDQIjl

Upvotes: 1

Related Questions