SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    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.

  3. #3
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vgarcia View Post
    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.

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    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.

  5. #5
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    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.
    yes validations go in the model but for example a form to submit login credentials...

  6. #6
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •