ruby - How to test if parameters exist in rails -


i'm using if statement in ruby on rails try , test if request parameters set. regardless of whether or not both parameters set, first part of following if block gets triggered. how can make part triggered if both params[:one] , params[:two] set?

if (defined? params[:one]) && (defined? params[:two])  ... ... elsif (defined? params[:one])  ... ... end 

you want has_key?:

if(params.has_key?(:one) && params.has_key?(:two)) 

just checking if(params[:one]) fooled "there nil" , "there false" value , you're asking existence. might need differentiate:

  • not there @ all.
  • there nil.
  • there false.
  • there empty string.

as well. hard without more details of precise situation.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -