ruby-on-rails - Rails 循环遍历数据库表并按名称拒绝一个对象
问题描述
我正在根据以下内容在数据库表中搜索对象id
:
def edit
@attachments = Attachment.where(item_id: @item.id)
end
在视图内部,我试图拒绝这样的no-image.jpg
图像:
<% @attachments.reject { |attachment| attachment.image.file.identifier == "no-image.jpg" }.each do |attachment| %>
<%= image_tag(attachment.image.url(:mini))%>
<%= link_to "Remove", remove_item_attachment_path(attachment), :data => {:confirm => 'Are you sure?'}, :method => :delete %>
<% end %>
现在即使no-image.jpg
当我调试它时文件存在于循环中,它也会给出以下错误:
NoMethodError in Items#edit
undefined method `identifier' for #<CarrierWave::Storage::Fog::File:0x007fb193633520>
错误在这一行:
<% @attachments.reject { |attachment| attachment.image.file.identifier == "no-image.jpg" }.each do |attachment| %>
有什么想法可以解决这个问题吗?
解决方案
看到你也写attachment.image.url
我假设image
是图像存储在Attachment
类中的字段,所以在这种情况下你应该写
attachment.image.identifier
我觉得attachment.image_identifier
也是可以的。
推荐阅读
- asp.net-core - 身份核心 - 更改密钥类型后无法登录
- python - 熊猫,对象列上的尾随 .0
- python-3.x - 使用 Python 和 exchangelib 连接到内部 SMTP 服务器的正确方法是什么
- user-interface - Meshlab - 可以导出/导入当前相机配置吗?
- reactjs - 如何使用 AsyncStorage React Native 将一条记录存储到现有数组中
- python - 以下 SQL 语句的 Django 查询是什么?
- javascript - 模态文本在其他模态中重复
- r - 使用 readr::read_csv() 时如何使用 select_helpers() [starts_with()]
- r - 根据两列值的计算来过滤数据表的优雅方法是什么?[R]
- javascript - 汇总“未解决的依赖项”和“缺少全局变量名”错误,但未引用特定依赖项