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
Post a Comment