2017-02-03 7 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変数は、ここに示されたこれらの最初の2を超えるすべての10人の担当者を通じて個々のループを続けます。これらのハッシュ内の唯一の2つの変数は、 'build_markers(@ rep#)'呼び出しと、各ユーザーのマーカーの頭文字と色を示す 'chld = TP | 81DF08 | 000000'呼び出しです。私は初心者なので、最初から完全に間違っている可能性があります。アドバイスをいただければ幸いです。ありがとう!

EDIT - で、それは変更に必要な唯一のハードコーディングされた変数だったので、私のユーザーテーブルに「マーカー」列を追加するような単純なことになった>

私の連結コード、マップマーカー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

これは単純にDBからレコードを取得することです行うには良い方法コレクション全体を1つに保存するインスタンス変数。これを行う

@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") 

は、パフォーマンスのために恐ろしいです。あなたがルビーで新しい人なら、私はhttp://tryruby.orgのようなことをやって、もっと複雑な問題を解決しようとする前に配列とハッシュと基本を操作する方法を学ぶことをお勧めします。

+1

コードにIDをハードコードするのは、本当に悪いコードの匂いです。これは、IDがデータベースによって順番に作成されるため、コードをテストできないことを意味します。代わりに、どのリソースを利用できるようにするべきか、それをフィルタリングする賢明な方法について考える必要があります。 – max

+0

この方法では、残念ながらマーカーの色が制限されます。なぜなら、各ユーザーの地図上に動的マーカー色を設定する必要があるからです。 –

+1

あなたは 'store.user'によってユーザを得ることができるはずです - あなたはusersテーブルに色を保存できます。あなたがやっていることは、本当にアプリケーションを構築するための実現可能な方法ではありません... – max