我試圖本法干擾適用於@ post.author的觀點,但我得到未定義的方法'驗證」的零:NilClass
undefined method 'verified' for nil:NilClass
def showVerifiedIcon(user)
u = User.where(username: user).first
if u.verified?
'<i class="fa fa-check-circle verified-icon fa-lg"></i>'
else
end
end
'User.find_by(...)'比更簡潔'User.where(...)。first'。 – tadman 2014-11-05 20:47:37
確定在我的答案中編輯它,我會接受 – 2014-11-05 20:52:01
我個人不喜歡'try',每當我在代碼庫中找到它時,它就像是一種代碼味道。原因是,當你使用嘗試,你沒有任何信心,如果你有你的對象不。它違反了「告訴,不要求原則」。 – Barbared 2014-11-05 21:48:49