我有一张基于美国州的标记的地图。每个州都有n个城市。
我有一个州模型,控制器和城市模型,控制器。
当我点击状态标记时,我希望在信息窗口中显示城市列表。
所有这些信息都显示在主页上。
这是我到目前为止所做的: -
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
州/ _gmaps4rails_infowindow.html.haml
=@state.cities.each do |city|
=city.name
不用说它不起作用。有人可以帮帮我吗?
答案 0 :(得分:1)
好吧,你的home_controller.rb
没问题。你在这里写的,你想使用一个名为object
的局部变量的部分。
在部分本身中,你写道:
=@state.cities.each do |city|
=city.name
实例变量没有在那里定义,你在上面定义了一个局部变量。
替换为:
=object.cities.each do |city|
=city.name
从那里它应该工作。
注意:
def gmaps4rails_infowindow
@state = Gmaps.map.markers
end
是:
无用:您在控制器中定义了infowindow
错误:Gmaps.map.markers
仅作为js变量生存