2010-11-26 66 views
2

在我的Shop(名稱= ABC; ID = 10)顯示視圖中,我想顯示附近的商店。因此,我用這個:排除軌道中的相同項目

  <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?

回答

9

使用reject

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

然後在other_shops迭代:

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

輝煌!非常感謝! – Victor 2010-11-26 08:49:31

1

當你在第一位置設置@shops,使用一個額外條件"id <> ?", shop.id