1

我正在尝试在模型中调用图像标签并返回图像(如果存在),否则返回 null,如下所示:-

 def medium_avatar_exists?
    if self.avatar.present?
      image_tag self.avatar.thumb_medium_url
    else
      image_tag "missing-avatar-medium.png"
    end
  end

当我从视图中调用此方法时:-current_user.medium_avatar_exist?

我收到一条错误消息,提示未定义方法 image_tag 可能是什么问题?

4

1 回答 1

3

您不能在模型中使用辅助方法image_tag是辅助方法,并且您试图在模型中使用它,因此它会给出错误。

尝试在您application_helper.rb或您想要的其他帮助者中关注

def medium_avatar_exists?(user)
  if user.avatar.present?
    image_tag user.avatar.thumb_medium_url
  else
    image_tag "missing-avatar-medium.png"
  end
end

要不就

def medium_avatar_exists?(user)
  image_tag (user.avatar.present? ? user.avatar.thumb_medium_url : "missing-avatar-medium.png")
end
于 2012-09-17T04:42:14.397 回答