SuBzer0 SuBzer0 - 3 years ago 142
Javascript Question

Loading Javascript After Google Maps API Rails APP

Yesterday in another question, with the help of another user, I can managed to use the Geocoder and Gmaps4rails gem. It is working just fine, but after I add the script on the page, the others Javascript doesn't work any more.

So if I refresh the page works perfect, Fotorama and the Gmaps works, but, when I am redirected by another link on the application Fotoroma doesn't load, actually only Gmaps work. After this happened in the entire application the others JS stopped work.

The view where the GMaps are:

<script src="//"></script>
<script src="//"></script>
<script src='//' type='text/javascript'></script>

<div class="fotorama col-sm-6"
<% @property.images.each do |imagem| %>
<%= link_to imagem.url do %>
<%= image_tag(imagem.url) %>
<% end if @property.images? %>
<% end %>
<h3>Description of the property:</h3>
<%= @property.description %>
<h3>Property in the map:</h3>
<div class="col-sm-10">
<div style='width: 800px;'>
<div id="map" style='width: 800px; height: 400px;'></div>

At the bottom the JS Scripts:

<script type="text/javascript">
$(document).on('turbolinks:load', function(){
<script type="text/javascript">
handler ='Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers(<%=raw @hash.to_json %>);

The application JS:

//= require bootstrap
//= require underscore
//= require gmaps/google
//= require turbolinks
//= require fotorama
//= require_tree .

The layout of application

<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>

I already try to change the include tag for:

<%= javascript_include_tag 'application', 'data-turbolinks-track' => 'reload' %>

Don't solve the problem. If I deleted from the view the JS importation scripts, the map doesn't work, but the JS problems stop.

I try to in the importation of gmaps do this:

<script src="//" data-turbolinks-eval=false></script>

Doesn't work to.

When the problem happen, the console shows this:

enter image description here

Anyone had this problem or know how to solve it?

Answer Source

I can managed to workaround this problem. I don't think this is the solution, but if anyone have the same issue, maybe can help.

First needs to correct the link of the source of the script. Changing of

<script src="//"></script>


<script src=""></script>

I find this by the comment of stevenm here:

And also change the location of the Scritps to the head of


It should be like this:

<%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => 'reload' %>
<script type='text/javascript' src="//"></script>
<script src=""></script>
<script type='text/javascript' src='//'></script>
<%= csrf_meta_tags %>

After this, at least for me, works just fine.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download