What am I doing wrong with this? :)

Hi,

I’m having trouble with a piece of code based on an example someone
kindly posted here:

errors.add(‘surname’, “must be blank when the serial number is known”)
if !surname.nil?|| !surname==""

Now, even if surname is empty, it still flags the error message. I
imagine I’m doing something rather obviously wrong but I’m not sure what
:slight_smile:

Thanks in advance!

Hi,
You should be able to shorten your statement down to this.

errors.add(‘surname’, “must be blank when the serial number is known”)
if !surname.blank?

Other than that, it should work. I’d have a look at what’s getting
passed in for surname. Maybe change
your error message to:

errors.add(‘surname’, “must be blank when the serial number is known.
You are passing in >>#{surname}<<”) if !surname.blank?

That way you can make sure that nothing is getting passed in.

Cheers,
Eric G.

Make sure the errors.add and the if are on the same line.