>我正在循环访问一个数组并显示图像:
<% @attachments.each do |attachment| %>
<%= image_tag(attachment.image.url(: thumb))%>
<%= link_to "Remove", remove_item_attachment_path(attachment),
data: { confirm: "Are you sure" },
:method => :delete %>
这是我在控制器中拥有的内容,以便查找特定项目的附件:
@attachments = Attachment.where(item_id: @item.id)
附件表中的图像列是一个string
我希望循环跳过并且不显示我存储在数据库中的特定图像,而是显示所有其他图像!
我尝试使用break if
或next if
条件,但没有任何运气:
<% @attachments.each do |attachment| %>
<% next if attachment.image == "no-image.jpg" %>
<%= image_tag(attachment.image.url(:thumb))%>
<%= link_to "Remove", remove_item_attachment_path(attachment),
data: { confirm: "Are you sure" },
:method => :delete %>
或
<% @attachments.each do |attachment| %>
<% break if attachment.image == "no-image.jpg" %>
<%= image_tag(attachment.image.url(:thumb))%>
<%= link_to "Remove", remove_item_attachment_path(attachment),
data: { confirm: "Are you sure" },
:method => :delete %>
关于如何实现这一点的任何想法?
更新 1
这是我从控制台中得到的attachment.image
#<AttachmentUploader:0x007f8b932016f8 @model=#<Attachment id: 333, item_id: 136, account_id: nil, image: "no-image.jpg", created_at: "2018-03-19 16:59:01", updated_at: "2018-03-19 16:59:01">, @mounted_as=:image, @storage=#<CarrierWave::Storage::File:0x007f8b93201540 @uploader=#<AttachmentUploader:0x007f8b932016f8 ...>>, @file=#<CarrierWave::SanitizedFile:0x007f8b93200d48 @file="/Users/RubymineProjects/public/uploads/attachment/image/333/no-image.jpg", @original_filename=nil, @content_type=nil>, @versions={:thumb=>#<AttachmentUploader::Uploader70118693643120:0x007f8b93200c58 @model=#<Attachment id: 333, item_id: 136, account_id: nil, image: "no-image.jpg", created_at: "2018-03-19 16:59:01", updated_at: "2018-03-19 16:59:01">, @mounted_as=:image, @parent_version=#<AttachmentUploader:0x007f8b932016f8 ...>, @storage=#<CarrierWave::Storage::File:0x007f8b93200b40 @uploader=#<AttachmentUploader::Uploader70118693643120:0x007f8b93200c58 ...>>, @file=#<CarrierWave::SanitizedFile:0x007f8b93200578 @file="/Users/RubymineProjects/public/uploads/attachment/image/333/thumb_no-image.jpg", @original_filename=nil, @content_type=nil>, @versions={}>, :mini=>#<AttachmentUploader::Uploader70118685692560:0x007f8b93200c30 @model=#<Attachment id: 333, item_id: 136, account_id: nil, image: "no-image.jpg", created_at: "2018-03-19 16:59:01", updated_at: "2018-03-19 16:59:01">, @mounted_as=:image, @parent_version=#<AttachmentUploader:0x007f8b932016f8 ...>, @storage=#<CarrierWave::Storage::File:0x007f8b932004b0 @uploader=#<AttachmentUploader::Uploader70118685692560:0x007f8b93200c30 ...>>, @file=#<CarrierWave::SanitizedFile:0x007f8b932034a8 @file="/Users/RubymineProjects/public/uploads/attachment/image/333/mini_no-image.jpg", @original_filename=nil, @content_type=nil>, @versions={}>}>
如果您使用回形针,则有默认方法来查找文件名original_filename
<% @attachments.each do |attachments| %>
<% if attachment.image.original_filename != "no-image.jpg" %>
<%= image_tag(attachments.image.url(:thumb))%>
<%= link_to "Remove", remove_item_attachment_path(attachments),
data: { confirm: "Are you sure" },
:method => :delete %>
<% end %>
只需使用"拒绝"方法。
<% @attachments.reject { |attachment| attachment.image == "no-image.jpg" }.each do |attachment| -%>
...
<% end -%>
编辑:
根据下面和上面的评论,原始海报正在使用Carrierwave来存储文件。 因此,"attachment.image"是一个Carrierwave上传器对象。
<% @attachments.reject { |attachment| attachment.image.identifier == "no-image.jpg" }.each do |attachment| -%>
...
<% end -%>
<</div>
div class="one_answers"> 好的,我去结合迈克尔·查尼和 prasanthrubyist 的答案
<% @attachments.reject { |attachment| attachment.image.file.identifier != "no-image.jpg" }.each do |attachment| -%>
您也可以通过原始查询在数据库中执行此操作。通常,最好尽量将这种逻辑排除在视图之外。
@attachments = Attachment.where(item_id: @item.id).where.not(image: 'no-image.jpg')
或者,如果数据库值为 nil
:
@attachments = Attachment.where(item_id: @item.id).where.not(image: nil)
我可能会将其添加为模型上的范围,以便您可以执行以下操作:
@attachments = @item.attachments.with_image