I have an optional URL param, say "user_id" I need to check for. I know I can use
if params.has_key?(:user_id) ...
to do things based on the presence of the user_id
parameter, but sometimes user_id
is passed without a value, so I want to ignore it completely. To fight the issue, I find myself doing this a lot--but there's got to be a better way, right?
if params[:user_id] && !params[:user_id].empty?
# Do stuff
end
It just seems really ugly.
If you're just checking if params[:user_id]
is present, then you can try:
if params[:user_id].present?
# do stuff
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With