How to create routes, which accepts only allowed values?

Asked

Viewed 74 times

1

How do I make :status accept only allowed values?

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

Currently :status accepts anything I submit.

I wish :status to be only: enabled or disabled.

It has a kind of :status in(enabled, disabled)?

1 answer

1


You can do it 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)" }

It has to come within the option constraints, which is a hash, where you place the parameter, pointing to the regular expression it accepts.

That expression I made is the right one for your case.

If the url for /para_activated or /para_deactivated, the route is identified, and calls the controller to continue. If the url for /parabla_blablablabla, return page not found.

  • Perfect that’s right!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.