Mel Mel - 3 months ago 29
Javascript Question

Rails 4 - Google Maps with gmaps4rails gem

I am trying to figure out how to use gmaps4rails gem in my Rails 4 app.

I have a view partial in which I have inserted the 3 script tags shown in the documentation, replacing the API key with my own (which is stored in my environment secrets file).

<script src="//maps.google.com/maps/api/js?v=3.23&key=ENV['GOOGLE_MAPS_API_KEY']"></script>


I think I may have done this incorrectly, because I can't currently load maps. The error message I see when I inspect the console from Chrome is:

util.js:220 Google Maps API warning: InvalidKey https://developers.google.com/maps/documentation/javascript/error-messages#invalid-key


Can anyone see if I have made an error in the way I have inserted my API key to this script?

Vic Vic
Answer

You are inserting a ruby snippet into a .html.erb file, so u need the <%=%> to interpolate the ruby string

<script src="//maps.google.com/maps/api/js?v=3.23&key=<%= ENV['GOOGLE_MAPS_API_KEY'] %>"></script>

Check inspect element of ur html page to see that the url is interpolated correctly.

Comments