I have a newish Rails 4 app which contains no custom Javascript - the folders app/assets/javascripts
, lib/assets/javascripts
and vendor/assets/javascripts
are all empty (apart from app/assets/javascripts/application.js
).
When I click around the app I keep getting the error jquery-ujs has already been loaded!
in the JS console. It happens every time I hit the "back" button, and sometimes when I click a normal link (although I can't make it happen consistently.)
application.js:
//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require turbolinks
//= require_tree .
When I remove turbolinks from application.js, I stop getting the error... so turbolinks appears to be the culprit.
Is this a bug with turbolinks, or am I doing something wrong?
Here's my Gemfile:
source 'https://rubygems.org'
ruby '2.0.0'
gem 'rails', '4.0.0'
gem 'active_model_serializers'
gem 'aws-sdk'
gem 'bootstrap-sass-rails'
gem 'coffee-script-source', '1.5.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'devise'
gem 'factory_girl_rails', '4.1.0'
gem 'figaro'
gem 'font-awesome-sass-rails'
gem 'jbuilder', '~> 1.2'
gem 'jquery-rails'
gem 'paperclip'
gem 'paperclip-meta', github: 'y8/paperclip-meta'
gem 'pg'
gem 'sass-rails', '~> 4.0.0'
gem 'turbolinks'
gem 'uglifier', '>= 1.3.0'
group :production do
gem 'rails_12factor'
end
group :doc do
gem 'sdoc', require: false
end
group :test do
gem 'capybara', '2.1.0'
end
group :development, :test do
gem 'childprocess', '0.3.9'
gem 'debugger'
gem 'faker', '~> 1.2.0'
gem 'json_spec'
gem 'rspec-rails'
gem 'rspec-mocks'
end
group :development do
gem 'annotate'
gem 'hirb'
gem 'wirb'
end
Changing around the order of the require
s in application.js doesn't seem to help either.
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 thedata-turbolinks-eval
attribute tofalse
:
<script type="text/javascript" data-turbolinks-eval=false>
console.log("I'm only run once on the initial page load");
</script>