当前位置: 动力学知识库 > 问答 > 编程问答 >

methods - Rails: How to check for attributes with polymorphic? (Prints okay, won't allow conditional)

问题描述:

Going back to accessing user attributes (name, e-mail, avatar, etc) for the comments section ("identify user as commenter"), why does it let me print out some values but not use them in conditionals?

Why can I do this? (Prints the avatar next to the comment as it should)

<%= image_tag comment.user.avatar.url, size: "64" %>

But not this? (Won't let me check if there's an avatar or not first)

<% if comment.user.avatar.url.empty/blank/nil/present? %>

So if a commenter hasn't uploaded an avatar yet, it returns:

undefined method \'avatar\' for nil:NilClass

Is there a method I need to define in a controller, or a scope in a model, or is there another way of checking in this situation?

Thank you.

分享给朋友:
您可能感兴趣的文章:
随机阅读: