如果 Rails 中的项目相同,则排除 [英] Exclude if same item in Rails

查看:38
本文介绍了如果 Rails 中的项目相同,则排除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Shop (name = ABC; ID = 10) 显示视图中,我想显示附近的商店.因此,我使用这个:

In my Shop (name = ABC; ID = 10) show view, I want to show the nearby shops. Hence, I use this:

          <h1>
            Shops nearby <span id="title"><%=h @shop.name %></span>
          </h1>

<% @shops.each_with_index do |shop, i| %>
                    <% if shop.geocoded? %>
                    {
                      latitude: <%= shop.lat %>,
                    longitude: <%= shop.lng %>,
                      html: "<a href='#item_<%= i + 1 %>'><strong><%=h shop.name %></strong></a>",
                      icon: { image: "<%= APP_CONFIG[:site_url] + '/images/map_blue_' + (i+1).to_s + '.png' %>",
                              iconsize: [48, 48],
                              iconanchor: [24,48],
                              infowindowanchor: [24, 0] }
                    },
                    <% end %>
                  <% end %>

你会注意到h1实际上显示了当前商店ABC,而each的其余部分实际上显示了它附近的所有商店.问题是,它还包括商店 ABC,因为它是最近的点.我如何要求 each 排除商店 ABC?

You will notice that the h1 actually shows the current shop ABC, and the rest of each actually shows all the shops nearby it. The problem is, it also includes the shop ABC as it is the nearest point. How do I ask the each to exclude shop ABC?

推荐答案

使用 <代码>拒绝:

other_shops = @shops.reject {|shop| shop == @shop}

然后遍历other_shops:

other_shops.each_with_index do |shop, i|
  ...

这篇关于如果 Rails 中的项目相同,则排除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆