Turbolinks causing a "jquery-ujs has already been loaded!" error

The most likely explanation is that you're including your scripts in the page body.

See this issue for more: https://github.com/rails/turbolinks/issues/143

From the turbolinks readme:

As a rule of thumb when switching to Turbolinks, move all of your javascript tags inside the head and then work backwards, only moving javascript code back to the body if absolutely necessary. If you have any script tags in the body you do not want to be re-evaluated then you can set the data-turbolinks-eval attribute to false:

<script type="text/javascript" data-turbolinks-eval=false>
  console.log("I'm only run once on the initial page load");
</script>

For me, I had included these written twice so it was loading twice. Once in application.js //= require jquery and once in application.html.erb as a script tag.


I faced the same issue and after doing some hit and trial i found the sequence of javascript files matters.

The working sequence is

//= require jquery
//= require jquery_ujs
//= require jquery.turbolinks
//= require turbolinks

Hope, this solution will help someone.


The problem that I had It was that I copied the <%= render 'layouts/head'%> twice!, in the head and in the body. check the laouts/application.html.erb file