2017-02-03 24 views
0

我在我的一個控制器中有一些重複的代碼,我想合併成一個循環,但是我找不到任何明確的指示,因爲這似乎是一個獨特的gmaps4rails的用例。我試圖創建一個實例變量的數組,但它似乎並沒有工作!我想代碼鞏固:實例控制器內部的可變循環

stores_controller.rb - >

class StoresController < ApplicationController 

    def map 
     @rep1 = Store.where(:user_id => "1") 
     @rep2 = Store.where(:user_id => "10") 
     @rep3 = Store.where(:user_id => "11") 
     @rep4 = Store.where(:user_id => "12") 
     @rep5 = Store.where(:user_id => "13") 
     @rep6 = Store.where(:user_id => "14") 
     @rep7 = Store.where(:user_id => "15") 
     @rep8 = Store.where(:user_id => "16") 
     @rep9 = Store.where(:user_id => "17") 
     @repA = Store.where(:user_id => "18") 

     @hash1 = Gmaps4rails.build_markers(@rep1) do |store, marker| 
     marker.lat store.lat 
     marker.lng store.long 
     marker.title store.name 
     marker.infowindow "#{store.store_infowindow}" 
     marker.picture({ 
      :url => "//chart.apis.google.com/chart?chst=d_map_pin_letter&chld=TP|81DF08|000000", 
      :width => 52, 
      :height => 32 
     }) 

     end 
     @hash2 = Gmaps4rails.build_markers(@rep2) do |store, marker| 
     marker.lat store.lat 
     marker.lng store.long 
     marker.title store.name 
     marker.infowindow "#{store.store_infowindow}" 
     marker.picture({ 
      :url => "//chart.apis.google.com/chart?chst=d_map_pin_letter&chld=BS|267AD2|D9E1FF", 
      :width => 52, 
      :height => 32 
     }) 
     end 
     @hash3 = etc, etc, etc... 

我還會包括我的地圖上JS的良好措施視圖文件標記裝載機,

map.html.erb - >

markers = handler.addMarkers(<%=raw @hash1.to_json %>), handler.addMarkers(<%=raw @hash2.to_json %>), 
      handler.addMarkers(<%=raw @hash3.to_json %>), handler.addMarkers(<%=raw @hash3.to_json %>), 
      handler.addMarkers(<%=raw @hash4.to_json %>), handler.addMarkers(<%=raw @hash5.to_json %>), 
      handler.addMarkers(<%=raw @hash6.to_json %>), handler.addMarkers(<%=raw @hash7.to_json %>), 
      handler.addMarkers(<%=raw @hash8.to_json %>), handler.addMarkers(<%=raw @hash9.to_json %>), 
      handler.addMarkers(<%=raw @hashA.to_json %>); 

的gmaps4rails標誌建築@hash變量繼續通過所有10個代表其個人環路超越在此間表示這些第2位。這些哈希中唯一的兩個變量是'build_markers(@ rep#)'調用和'chld = TP | 81DF08 | 000000'調用,它指示每個用戶的標記的首字母和顏色。我是一名初學者,所以我知道我從一開始就可能完全做錯了!任何建議表示讚賞。謝謝!

編輯 - >

我統一代碼,它結束了,添加一個「標記」列到我的用戶表那麼簡單,因爲這是一個需要改變的唯一硬編碼的變量,在的地圖標記URL中「#{} store.user.marker」形式:

stores_controller.rb - >

def map 
    @stores = Store.all 
    @hash = Gmaps4rails.build_markers(@stores) do |store, marker| 
    marker.lat store.lat 
    marker.lng store.long 
    marker.title store.name 
    marker.infowindow "#{store.store_infowindow}" 
    marker.picture({ 
     :url => "//chart.apis.google.com/chart?chst=d_map_pin_letter&chld=#{store.user.marker}", 
     :width => 52, :height => 32 
    }) 
    end 
    respond_to do |format| 
    format.html 
    format.json { render json: @hash } 
    end 
end 

回答

0

一個更好的辦法來做到這一點是簡單地從數據庫中獲取記錄並將整個集合存儲在一箇中實例變量。

@stores = Store.where(user_id: [1, 2, 3]) 

@markers = Gmaps4rails.build_markers(@stores) do |store, marker| 
    marker.lat store.lat 
    marker.lng store.long 
    marker.title store.name 
    marker.infowindow "#{store.store_infowindow}" 
    # if they need different pictures handle it in the model 
    marker.picture({ 
      :url => "//chart.apis.google.com/chart?chst=d_map_pin_letter&chld=TP|81DF08|000000", 
      :width => 52, 
      :height => 32 
    }) 
end 

這樣做:

@rep1 = Store.where(:user_id => "1") 
    @rep2 = Store.where(:user_id => "10") 
    @rep3 = Store.where(:user_id => "11") 
    @rep4 = Store.where(:user_id => "12") 
    @rep5 = Store.where(:user_id => "13") 
    @rep6 = Store.where(:user_id => "14") 
    @rep7 = Store.where(:user_id => "15") 

是可怕的性能,因爲每一行都會創建一個單獨的數據庫查詢。如果你是ruby的新手,我會建議你做一些像http://tryruby.org這樣的工作,並學習如何在嘗試解決更復雜的問題之前操作數組和哈希。

+1

將ID硬編碼到代碼中是一種非常糟糕的代碼異味。這意味着您將無法測試代碼,因爲ID是由數據庫按順序創建的。相反,您應該考慮應該提供哪些資源以及對它們進行過濾的明智方法。 – max

+0

這種方法會限制標記的顏色,但不幸的是,因爲我需要在地圖上爲每個用戶使用動態標記顏色。 –

+1

您應該可以通過'store.user'獲取用戶 - 您可以將顏色存儲在用戶表中。你在做什麼並不是構建應用程序的真正可行方法...... – max