Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to require and check url parameters in rails 3

Is there a way in your routes file to check and validate URL parameters. I am NOT talking about restful '/controller/action/:id' params, but 'controller/action?param1=x&param2=y&param3=z'. I need to be able to validate each parameter and require them.

like image 996
pho3nixf1re Avatar asked Dec 28 '22 00:12

pho3nixf1re


1 Answers

Yes, you can. For example to check that param1 exists and is not blank you would do the following:

match 'c/action' => 'c#action', :constraints => lambda{ |req| !req.params[:param1].blank? }

You can also scope these constraints to apply them to multiple routes:

scope :constraints => lambda{ |req| !req.params[:param1].blank? } do
  match 'controller/action1' => 'controller#action1'
  match 'controller/action2' => 'controller#action2'
end
like image 177
Pan Thomakos Avatar answered Jan 12 '23 15:01

Pan Thomakos