Rails:遍历并跳过特定对象

时间:2018-03-20 12:31:26

标签: ruby-on-rails ruby

我正在遍历数组并显示图像:

<% @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 ifnext 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={}>}>

4 个答案:

答案 0 :(得分:3)

只需使用“拒绝”方法。

<% @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 -%>

答案 1 :(得分:3)

如果您使用了回形针,则可使用默认方法查找文件名 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 %>

答案 2 :(得分:0)

好的,我把它与Michael Chaney和prasanthrubyist的答案结合起来

<% @attachments.reject { |attachment| attachment.image.file.identifier != "no-image.jpg" }.each do |attachment| -%>

答案 3 :(得分:0)

您也可以通过原始查询在数据库中执行此操作。通常最好尝试将这种逻辑排除在视图之外。

@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