【发布时间】:2012-01-23 13:42:57
【问题描述】:
此代码正常工作(使用 HAML):
#comments
- @blog.comments.each do |comment|
.comment
.username
- if !eval("comment.user").nil?
#{comment.user.email}
.content
#{comment.content}
但是,如果我删除“eval”行,即
#comments
- @blog.comments.each do |comment|
.comment
.username
#{comment.user.email}
.content
#{comment.content}
我收到一个错误:
undefined method `email' for nil:NilClass
即使数据库中没有 cmets(因此不应评估循环内容)。怎么回事?
【问题讨论】:
-
- if !eval("comment.user").nil?这很难看,就用- if comment.user -
@MikhailNikalyukin thx,我一直在寻找更简洁的语法。我以为我以前尝试过,但现在可以了:)
标签: ruby-on-rails ruby haml