country attribute’s default value is nil.
In countries table, some record has image_url, and the rest of the record’s country attributes are nil.
So I coded this in helper
def image(user)
if user.country.image_url
image_tag "flags/#{user.country.image_url}.png"
end
end
However, it returns error when image_url was nil
Something went wrong
How can I fix?
You’ll need two conditions: The user has to have a country, and that country has to have an image_url. Only then will there be something to show. Luckily, it’s a simple tweak:
If you’re paranoid, you should make sure that
userisn’tnileither.Hope that helps!