t56k
t56k

Reputation: 7001

Rails: array is blank if one field is blank

Why would this return blank if just one field is left unfilled? Doesn't make any sense to me.

def api_user_information
  user_information = "#{user.address}\n\n"
  user_information << "<b>P</b> #{user.phone}\n" unless user.phone.blank?
  user_information << "<b>F</b> #{user.fax}\n" unless user.fax.blank?
  user_information << "<b>M</b> #{user.mobile}\n" unless user.mobile.blank?
  user_information << "<b>E</b> #{user.email}\n" unless user.email.blank?
  user_information << "<b>W</b> #{user.url}\n" unless user.url.blank?
  user_information << "<b>Facebook</b> #{user.facebook}\n" unless user.facebook.blank?
  user_information << "<b>Twitter</b> @#{user.twitter}" unless user.twitter.blank?
end

Upvotes: 0

Views: 74

Answers (1)

lalameat
lalameat

Reputation: 754

The last statment result is your return value of the method. So the return value is result of

user_information << "<b>Twitter</b> @#{user.twitter}" unless user.twitter.blank?

if "user.twitter.blank?" is ture, "user_information << "Twitter @#{user.twitter}" is never reached. It might return "nil".

How about add

user_information 

or

return user_information 

in your last line of the method and see the result.

Upvotes: 4

Related Questions