How do I create routes that accept only the allowed values?

1

How do I: accept status only allowed values?

# routes.rb
get '/para_:status(/:opcao1)(/:opcao2)', to: 'search#index', :as => :search

Currently: status accepts anything I send.

I want the: status to only be: enabled or disabled.

Can you do some sort of: status in?

    
asked by anonymous 27.02.2014 / 00:05

1 answer

1

You can do this using regular expression, in your case it would look like this:

# routes.rb
get '/para_:status(/:opcao1)(/:opcao2)', to: 'search#index', as: :search, constraints: { status: "(ativado|desativado)" }

Constraints option, which is a hash , where you put the parameter, pointing to the regular expression it accepts.

This expression I made is the correct one for you.

If the url is / to_activated or / to_disabled , the route is identified, and calls controller to provide continuity. If url is / para_blablabla , return page not found.

    
27.02.2014 / 13:21