api - building a ruby conditional to check for nested params -
i'm curious in controller action, how can simple validation of nested params?
def create # validate incoming post request errors = array.new person = params[:person] event = params[:event] errors << "person email should not empty" if person[:email].blank? errors << "person name should not empty" if person[:name].blank? errors << "event name should not empty" if event[:name].blank?
this type of check barfing. i'm trying scan nested json params, example making post request on
"person": { "email":"foo@gmail.com", "name":"foo" },
this validate fine because nested name there. although if request without nested value, barf. how write conditional check nested value, , stuff in error value if it's empty. otherwise, if there no nested value continue normal.
you use has_key? method available on hash class.
errors << "person email should not empty" if person.has_key?(:email) && person[:email].blank?
Comments
Post a Comment