Shreya
Shreya

Reputation: 63

.nil? function not working in Ruby on Rails 5

def names
  if first_name.nil? && last_name.nil?
    errors.add(:first_name, "First Name and Last Name both can't be empty!")
  end
end

and even if I write so, it should allow either first or last name to be nil but not both. It doesn't work as expected

Upvotes: 0

Views: 206

Answers (1)

Andrii Makovenko
Andrii Makovenko

Reputation: 275

Use blank? or present? methods instead of nil?, they both check for empty strings and nil's

Upvotes: 4

Related Questions