【问题标题】:Rails: ReferenceError: google is not definedRails:ReferenceError:未定义谷歌
【发布时间】:2013-05-09 06:24:36
【问题描述】:

我的 application.js 上出现“ReferenceError: google is not defined”错误

这是我的 js 包含的内容:

$(document).ready(function(){
var input = document.getElementById('searchTextField');
var options = {
types: [],
componentRestrictions: {country: 'in'}
};
var autocomplete = new google.maps.places.Autocomplete(input, options);
});

不明白为什么会出现这个错误。我的搜索功能运行正常,但 Web 控制台出现此错误。

谁能告诉解决方案?

【问题讨论】:

  • 呃,您是在加载 Google 地图 API JS 文件吗?您可能需要发布更多代码,因为到目前为止您发布的内容中没有这些代码
  • @duncan 这就是我要发布的内容:"searchTextField", :class=> "homecatg" %>

标签: google-maps-api-3 ruby-on-rails-3.2


【解决方案1】:

正如duncan 所说,问题可能出在包含标签上。

我在显示页面的顶部包含了 GoogleMaps API(所以在 HEAD 标记之外)

<%= javascript_include_tag "https://maps.google.com/maps/api/js?key=#{ENV['GOOGLE_MAPS_API_KEY']}" %> 

摆脱这个

ReferenceError: google 未定义

我将包含 API 的位置从显示页面顶部更改为 layouts/application.html.erb 到 head 标签中。

<head>
<%= javascript_include_tag "https://maps.google.com/maps/api/js?key=#{ENV['GOOGLE_MAPS_API_KEY']}" %> 
</head>

【讨论】:

    【解决方案2】:

    请更改顺序: 第一个

    <script src="maps.googleapis.com/maps/api.... >
    

    然后

     <%= text_field_tag 'search', nil, :id=>"searchTextField", :class => "homecatg" %>
    

    在这种情况下,google 将在使用前定义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-13
      • 2012-08-28
      • 2018-05-13
      • 2020-09-20
      • 2014-07-27
      • 2016-07-07
      相关资源
      最近更新 更多