TypeError:this.clusterer在Rails模型名称更改后未定义

TypeError: this.clusterer is undefined after Rails Model name change

本文关键字:未定义 模型 this clusterer Rails TypeError      更新时间:2023-09-26

我已经更改了模型和控制器的名称以及所有连接的数据+文件名。一切正常,除了我的JS GoogleMaps API喊:

TypeError: this.clusterer is undefined

问题是,我再次尝试预编译我的资产,我搜索了所有的文件,但找不到答案。你知道rake持有/更改的所有资产在哪里吗?我应该在哪里寻找答案?

我的控制器:

def index
    @hunts = Hunt.all
    @hash = Gmaps4rails.build_markers(@hunts) do |hunt, marker|
        marker.lat hunt.latitude
        marker.lng hunt.longitude
        marker.infowindow hunt.email
    end
end

视图:

<script src="//maps.google.com/maps/api/js?v=3.13&amp;sensor=false&amp;libraries=geometry" type="text/javascript"></script>
<script src='//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js' type='text/javascript'></script>
<%= javascript_include_tag 'hunt_map'%>

和JS:

handler = Gmaps.build('Google');
handler.buildMap(
  { 
  provider: { 
    center: new google.maps.LatLng(50.0636153, 19.9317813), 
    zoom: 13
  }, 
  internal: { id: 'map' }
  }, 
  function(){
    markers = handler.addMarkers(<%=raw @hash.to_json %>);
    handler.fitMapToBounds();
    handler.addCircles(
      [{ lat: 50.0636153, lng: 19.9317813, radius: 5}],
      { strokeColor: "#000" });
  });
handler.addMarkers([
    {
      "lat": 50.0636153,
      "lng": 19.9317813,
      "picture": {
        "url": "http://www.tamu.edu/faculty/ccbn/dewitt/adp/resources/images/shamrock.gif",
        "width":  36,
        "height": 36
      },
      "infowindow": "Treasure lies here!"
    }
 ]);

更新:我的配置/初始化程序/assets.rb

Rails.application.config.assets.version = '1.0'
Rails.application.config.assets.precompile += %w( hunt_map.js )

您不能将数据传递到位于assets中的JavaScript。这就是为什么你在地图上看不到你的点,因为nil在这条线上通过

markers = handler.addMarkers(<%=raw @hash.to_json %>);

解决问题最简单的方法是将js直接放在viewer中。为了让它更优雅,请查看这一集。

发生此TypeError: this.clusterer is undefined是因为您试图在function(){...}之外调用handler.addMarkers(...)。只要把它移回那里,错误就会消失。