Reputation: 1365
I am getting "ReferenceError: google is not defined" error on my application.js
This is what my js contains:
$(document).ready(function(){
var input = document.getElementById('searchTextField');
var options = {
types: [],
componentRestrictions: {country: 'in'}
};
var autocomplete = new google.maps.places.Autocomplete(input, options);
});
Not getting why this error is coming. My search function is working properly but web console giving this error.
Can anybody tell solution on this?
Upvotes: 0
Views: 2887
Reputation: 52
Please, change the order: first
<script src="maps.googleapis.com/maps/api.... >
then
<%= text_field_tag 'search', nil, :id=>"searchTextField", :class => "homecatg" %>
In that case the google will be defined before used.
Upvotes: 0
Reputation: 69
As duncan said the problem can be with include tag.
I was including GoogleMaps API at top of show page (so out of HEAD tag) like that
<%= javascript_include_tag "https://maps.google.com/maps/api/js?key=#{ENV['GOOGLE_MAPS_API_KEY']}" %>
To get rid of this
ReferenceError: google is not defined
I changed the place where I included API from top of show page to layouts/application.html.erb
into head tag.
<head>
<%= javascript_include_tag "https://maps.google.com/maps/api/js?key=#{ENV['GOOGLE_MAPS_API_KEY']}" %>
</head>
Upvotes: 1