2012-05-12 63 views
0

我有一张基于美国状态得到标记的地图。每个州有n个城市。无法使用Gmaps4Rails渲染部分

我有一个状态模型,控制器和城市模型,控制器。

当我点击状态标记时,我想要在信息窗口中显示城市列表。

所有这些信息都出现在主页上。

这是我迄今所做的: -

home_controller.rb

def index 
    @states = State.all.to_gmaps4rails do |state,marker| 
     marker.infowindow render_to_string(:partial => "/states/gmaps4rails_infowindow", :locals => {:object => state}) 
     marker.json({:id => state.id}) 
    end 
end 

家/ index.html.haml

=gmaps({"map_options" =>{ "auto_zoom" => false, "zoom" => 3}, "markers" => { "data" => @states } }) 

state_controller.rb

def gmaps4rails_infowindow 
    @state = Gmaps.map.markers 
end 

states/_gm aps4rails_infowindow.html.haml

[email protected] do |city| 
    =city.name 

不用说,它不起作用。有人可以帮我吗?

回答

1

那么,你的home_controller.rb是好的。你在这里写你想要使用局部变量名为object的部分。

在局部本身,你写的:

[email protected] do |city| 
    =city.name 

实例变量没有定义,你定义一个局部变量的正上方。

替换:

=object.cities.each do |city| 
    =city.name 

从那里它应该工作。


注意:

def gmaps4rails_infowindow 
    @state = Gmaps.map.markers 
end 

是:

  • 没用:你在控制器中定义的信息窗口

  • 错误:Gmaps.map.markers只活的JS变量

+0

感谢它工作的很棒:),我将在未来几天内对你进行窃听,因为我正在使用你的宝石作为我的应用之一 –

+0

为什么它可以与对象一起工作? –

+0

,因为它是你给部分局部变量的名字。 – apneadiving