Results 1 to 6 of 6
Thread: blank or nil method?
Mar 4, 2007, 07:19 #1
blank or nil method?
for ruby a "blank" variable is a variable that either is "nil" or an empty string. Instead a "nil" variable is a variable that either has a "nil" value or doesn't exist 'cause no value has been assigned to it. So the question is: in rails when we develop application generally which method should we use when checking variables from forms? When a form field is submitted without a value no variable/params hash element is created so it is just right to use only "nil" method right? will we never get back an empty string from a form filed?
Mar 4, 2007, 07:43 #2
I'd use .blank? to check on a string, but it depends on your app I'd guess. I know for me an empty string usually means the same thing as nil, so .blank? makes sense. If an empty string means something different to your app than a nil value then use nil? instead.
Mar 4, 2007, 12:37 #3
Mar 4, 2007, 12:37 #4
- Join Date
- Aug 2005
- 0 Post(s)
- 0 Thread(s)
You should validate in your models, no need for blank? or nil? ;-).
What if they enter whitespace, which value do you get in the params hash?
I'd use blank? for user-entered data.
Mar 4, 2007, 14:48 #5
Mar 5, 2007, 02:21 #6
anyway if they enter whitespace you don't get an empy string.
At the end i think most of the time it's best to use .blank? 'cause it check for nil values and empty string...is that true?