首页 > 解决方案 > 在模型中使用 image_url 助手

问题描述

我正在尝试在我的模型中使用 image_url 助手。我在模型上也有一个 image_url 属性(无法更改)。当我调用 image_url 时,辅助方法似乎正在调用模型上的方法。我得到错误wrong number of arguments (given 1, expected 0)

 def public_image
    if self.avatar && photo_public?
      self.avatar.remote_url
    else
      image_url '/client/src/assets/images/icons/anon-small.svg'
    end
  end

标签: ruby-on-railsrubyhelpermethods

解决方案


image_url是一个视图助手,它不应该在模型中使用,你应该将该逻辑移动到视图的助手中

#application_helper.rb
def public_image(user)
  if user.avatar && user.photo_public?
    user.avatar.remote_url
  else
    image_url '/client/src/assets/images/icons/anon-small.svg'
  end
end

在您看来更改user.public_imagepublic_image(user)


推荐阅读