在模型中使用image_url helper

Ant*_*yrd 0 ruby ruby-on-rails helpermethods

我正在尝试在我的模型中使用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
Run Code Online (Sandbox Code Playgroud)

ari*_*uod 5

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
Run Code Online (Sandbox Code Playgroud)

在您的视图中更改user.public_imagepublic_image(user)