ruby-on-rails - 在模型中使用 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
解决方案
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_image
为public_image(user)
推荐阅读
- javascript - 在 angluarJS 中结合 ng-if 和 ng-click
- python - MongoDB 3.2.20 的 MongoEngine 版本
- angular - 如何观察 rxjs 中的属性变化?
- java - 在我的情况下,哪种 Firebase 数据结构行为最好?
- microsoft-graph-api - RelativePath 的 OneDrive DriveItem 的子项不起作用
- kotlin - 过滤、减去或分组 - 什么是最有效的?(科特林)
- javascript - TypeScript 中 JavaScript 函数的导出声明文件
- javascript - 如何从本地存储中删除表行
- javascript - JavaScript 画布和鼠标位置
- c# - 切换到窗口脚本不起作用