0

我希望我问这个问题是正确的,所以如果我离开了,请告诉我。

问题是试图建立一个从多个控制器中提取的主页,以显示来自多个控制器的最近位置,即。食品、企业等。

现在,各个列表页面都有从各自的列表中绘制的地图

@json = Controller.all.to_gmaps4rails

我该怎么做:

@json = Controller1 Controller2 .all.to_gmaps4rails

我希望这不是一个菜鸟问题,我只是度过了糟糕的一天。多谢你们!

编辑 12.5.2011 @seanhill - 这是模型之一,其他部分非常接近这种格式。首先,我什至不确定我的主页是否需要它自己的模型,因为它根本不与数据库交互,更多的是从执行工作的控制器中提取数据。感谢肖恩的回复!

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
4

3 回答 3

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控制器,但我认为您对事情的运作方式略有误解。假设您有一个Model被叫Dining和另一个被叫Shop,当您呼叫Dining.allShop.all在内部时class DiningsController < ApplicationController,您在呼叫或.allDining Model呼叫。Shop ModelDiningsController

您通过控制器显示的信息仅受您在其中调用的方法的限制,但最佳做法是确保显示信息的主要焦点与相应的控制器相关。

因此,您真正想做的是从多个模型中获取记录并将它们组合在一起以在单个地图中显示它们。

话虽如此,答案应该是这样的

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  
于 2011-12-05T04:29:29.380 回答
0
@json1 = something.to_gmaps4rails
@json2 = something.to_gmaps4rails

@json = (JSON.parse(@json1) + JSON.parse(@json2)).to_json
于 2013-01-04T19:24:34.040 回答
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

我希望这有帮助。

于 2013-03-14T16:10:12.660 回答