在控制器中组合变量

时间:2014-09-29 13:49:33

标签: ruby-on-rails ruby-on-rails-4 gmaps4rails gmaps4rails2

在网站上使用gmaps4rails进行地图功能:

这个想法是在地图上显示房子的标记加上多个景点。

我有这个代码;

控制器:

@house = House.friendly.find(params[:id])
@location = @house.location
@sights_markers = Location.where("category = 'sights'")

records = [@location, @sights_markers].compact

@hash = Gmaps4rails.build_markers(records) do |location, marker|
  marker.lat location.latitude
  marker.lng location.longitude

end

观点:

<div id="map" style='width: 100%; height: 500px; margin-top: 10px; margin-bottom: 20px;' ></div>

<script type=text/javascript> 

    handler = Gmaps.build('Google');
    handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
      markers = handler.addMarkers(<%=raw @hash.to_json %>);
      handler.bounds.extendWith(markers);
      handler.fitMapToBounds();


    });
</script>

我收到错误信息..找不到方法“纬度”

我做错了什么?

1 个答案:

答案 0 :(得分:1)

关键是要有一个数组。所以,替换:

records = [@location, @sights_markers].compact

records = @sights_markers.to_a + [@location]