Reputation: 21573
I have a textfield in a form with name="test"
. When I enter a value in, my params hash contains that value:
params[:test] => "value"
However, if I don't enter a value in, my params hash contains an empty string:
params[:test] => ""
My question is, how do I tell if the user manually entered an empty string or simply ignored/did not touch the field at all?
UPDATE Ok, let me explain the real issue, maybe I am not exapling myself well. I have bunch of search fields on a page, and I need to search based on them. This is what I am doing right now:
conditions = {}
conditions[:artist_name] = s_artist_name if s_artist_name && !s_artist_name.empty?
conditions[:city] = s_city if s_city && !s_city.empty?
I have to first check if it's nil
, and then check if it's emtpty?
. This doesn't look good to me, I don't know...it's too much work for such a simple task. I must be missing something . Or is this how it's done?
I check for nil
because if I call:
xyz.com/test
params will be nil.
If I press button on test
page without entering anything in the fields, params will be populated but with empty string.
So then I check for empty string as well.
Upvotes: 1
Views: 1500
Reputation: 3011
Checking
value.present? checks for both nil & empty and returns true if value is not nil and not empty.
value.blank? checks for both nil & empty and returns true if value is nil or empty.
Upvotes: 2
Reputation: 161
Maybe this post will help you Is There a Better Way of Checking Nil or Length == 0 of a String in Ruby?
Got many ways that people use to check for empty values, hope this helps.
Upvotes: 0