2011-12-01 38 views
0

我希望我問這個權利,所以請讓我知道如果我走了。試圖在一個頁面中填充多個json字符串的gmaps4rails

問題是試圖建立從多個控制器繪製一個主頁,顯示從多個控制器最近的位置,即食品,商業等。

眼下單獨列表頁面具有各自的

@json = Controller.all.to_gmaps4rails 

繪製地圖我怎麼會做這樣的事情:

@json = Controller1 Controller2 .all.to_gmaps4rails 

我希望這個心不是一個noob問題,我只是有不好的一天。多謝你們!

編輯2011年5月12日@seanhill - 這是車型之一,其他部分都非常接近這個格式。首先,我甚至不確定我的主頁是否需要它自己的模型,因爲它根本不與數據庫進行交互,更多的數據從控制器執行工作。感謝Sean的迴應!

class Dining < ActiveRecord::Base 
validates_uniqueness_of :name, :message => "already exists" 

attr_accessible :name, :address, :cuisine, :latitude, :longitude, :about, :facebook, :twitter, :phone, :website 
geocoded_by :address 
after_validation :geocode, :if => :address_changed? 

acts_as_gmappable :process_geocoding => false 

def gmaps4rails_address 
    "#{self.address}" 
end 

def gmaps4rails_infowindow 
    "<h3>#{self.name}</h3><br /><h5>#{self.cuisine}</h5>" 
end 

def self.search(search) 
    if search 
     where('name LIKE ?', "%#{search}%") 
    else 
     scoped 
    end 
end 

end 
+0

你是什麼意思的Controller1 Controller2?你不應該在控制器上調用'.all'。你應該在模型上做到這一點。你能給我們提供一些關於你的模型如何構建的信息嗎? –

+0

肖恩,對不起,我正在尋找從不同的控制器,即信息。商店和餐館,在一個主頁上的1個地圖。下面的答案有效,但我有興趣將信息從控制器移至模型。現在,模型幾乎是腳手架基礎,具有gmappable,_address和_infowindow以及內置於模型中的搜索。 – keade

回答

0

試試這個

holder = Controller1.all 
holder << Controller2.all 
@json = holder.flatten.map{|h| {lng: h.longitude, lat: h.latitude, class: h.class.to_s}}.to_json 

請務必更改longitudelatitude根據您的列名,並用js來操作基於類的標記。

由於@Sean Hill說你不應該在控制器上調用.all,但我認爲你對事物的工作方式有輕微的誤解。假設你有一個名爲ModelDining另一個叫Shop,當你調用內部class DiningsController < ApplicationControllerDining.allShop.all,你在呼喚.all無論是Dining ModelShop Model不上DiningsController

你通過控制器顯示的信息雖然是最佳實踐確保的顯示有關各自控制器的信息的主要焦點僅由你在它調用的方法的限制。

所以你真正要做的是從多個模型,並將它們組合得到記錄在一個單一的地圖來顯示它們。

隨着中說,答案應爲這樣的事情

holder = Dining.all # Takes all Dining records returned as an array and sets them to holder variable 
holder << Shop.all # Pushes the Shop records array into the holder with the dining records 
holder.flatten!# Next we flatten the array so we only have a single array. 

# Then we use the map method to run the given code one time for each instance 
# in the holder array to extract the info we need. The results for every instance 
# in holder are returned in an array which we then convert to_json. 
@json = holder.map{|h| {lng: h.longitude, lat: h.latitude, class: h.class.to_s}}.to_json 
+0

夥計。老闆!爲我工作就像一個魅力。那麼通過創建一個'持有者'並歸屬嵌套控制器,調用'持有者'將顯示下面的所有內容?很高興知道!謝謝喬伊 – keade

+0

我想這可以顯示每個位置,但我需要區分位置,即餐廳與商店。這種模式只允許大量顯示所有包含的鏈接? – keade

+0

正確,我已更新我的答案。 – Joey

0
@json1 = something.to_gmaps4rails 
@json2 = something.to_gmaps4rails 

@json = (JSON.parse(@json1) + JSON.parse(@json2)).to_json 
0

我填充地圖我節日的初始數據,然後使用此代碼添加搭乘它使用JavaScript,

<% content_for :scripts do %> 
    <script type="text/javascript"> 

    Gmaps.map.callback = function() { 

     $.getJSON('/rides_gmap', function(data){ 
      Gmaps.map.addMarkers(data); 
     }); 

     } 
    </script> 
<%end%> 

在遊戲機控制器我有這個

def rides_gmap 
    @rides = Ride.all 
    @json = @rides.to_gmaps4rails do |ride, marker| 
     marker.infowindow render_to_string(:partial => "/rides/infowindow", :locals => { :ride => ride}) 
     marker.picture({ 
       'picture' => view_context.image_path("orange-dot.png"), 
       'width' => 20, 
       'height' => 20 
       }) 
     marker.title "#{ride.address}" 
     marker.json({:ride_id => ride.id, :ride_festivaltype => ride.festival.festivaltype 
    end 
    respond_with @json 
    end 

我希望這有助於。

相關問題