javascript Turbolinks 导致“jquery-ujs 已经加载!” 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20684846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:01:47  来源:igfitidea点击:

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

javascriptjqueryruby-on-railsturbolinks

提问by GMA

I have a newish Rails 4 app which contains nocustom Javascript - the folders app/assets/javascripts, lib/assets/javascriptsand vendor/assets/javascriptsare all empty (apart from app/assets/javascripts/application.js).

我有一个新的 Rails 4 应用程序,它包含自定义 Javascript - 文件夹app/assets/javascriptslib/assets/javascripts并且vendor/assets/javascripts都是空的(除了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.)

当我点击应用程序时,我不断jquery-ujs has already been loaded!在 JS 控制台中收到错误消息。每次我点击“后退”按钮时都会发生这种情况,有时当我点击普通链接时(尽管我无法始终如一地发生这种情况。)

application.js:

应用程序.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.

当我从 application.js 中删除 turbolinks 时,我不再收到错误......所以 turbolinks 似乎是罪魁祸首。

Is this a bug with turbolinks, or am I doing something wrong?

这是 turbolinks 的错误,还是我做错了什么?

Here's my Gemfile:

这是我的 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 requires in application.js doesn't seem to help either.

更改requireapplication.js 中 s的顺序似乎也无济于事。

回答by Puhlze

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

最可能的解释是您将脚本包含在页面中body

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

有关更多信息,请参阅此问题:https: //github.com/rails/turbolinks/issues/143

From the turbolinks readme:

来自turbolinks 自述文件

As a rule of thumb when switching to Turbolinks, move all of your javascript tags inside the headand 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-evalattribute to false:

作为切换到 Turbolinks 时的经验法则,将所有 javascript 标签移到 里面head,然后向后工作,只有在绝对必要时才将 javascript 代码移回正文。如果您不想重新评估正文中的任何脚本标记,则可以将该data-turbolinks-eval属性设置为false

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

回答by lokeshjain2008

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

我遇到了同样的问题,在做了一些尝试和试验后,我发现 javascript 文件的顺序很重要。

The working sequence is

工作顺序是

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

Hope, this solution will help someone.

希望这个解决方案会对某人有所帮助。

回答by Fatima Haidar

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

对我来说,我已经把这些写了两次,所以它加载了两次。一次在 application.js 中//= require jquery,一次在 application.html.erb 中作为脚本标记。

回答by Carlos

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

我遇到的问题是我在头部和主体中复制了 <%= render 'layouts/head'%> 两次!检查 laouts/application.html.erb 文件