denniss
denniss

Reputation: 17589

Rails conditional routing

In my url, I want

localhost/12345 to be routed to /client/information/12345

However, I also want users to be able to call

/client to go to /client/index

I was not able to do this with

match ':id' => 'client#information'

because then the word 'client' would be set as the value of params[:id]

How can I create a route that will conditionally route to /client/information if the id is number, otherwise go to the /client/index

Upvotes: 0

Views: 3234

Answers (1)

Emily
Emily

Reputation: 18193

To add constraints to part of a route, you use the :constraints option. For instance:

match ':id' => 'client#information', :constraints => { :id => /\d+/ }

Check out the segment constraints section of the Rails Routing docs. It'll explain more details.

Upvotes: 2

Related Questions