本文介绍了在Rails 3.1基于资产的应用程序中包含两次javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然问题的标题与以前的标题非常相似,但我的问题似乎有所不同。

Although the title of the question is very similar to a number of previous ones, my issue seems different.

简而言之,第一项在js清单中包括两次。

Briefly, the first item in the js manifest is included twice.

这是我的 /app/assets/javascript/application.js Rails 3.1应用程序中的文件:

Here's the whole of my /app/assets/javascript/application.js file in a Rails 3.1 application:

//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require autocomplete-rails
//= require utilities

这里是渲染页面源代码的片段:

And here's a snippet of the rendered page's source:

<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery-ui.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/autocomplete-rails.js?body=1" type="text/javascript"></script>
<script src="/assets/utilities.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>

请注意,如果我移到 application.js 如下所示:

Please note that if I move up to the top any of the other lines in application.js like so:

//= require utilities
//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require autocomplete-rails

它始终是第一个包含两次的项目!

It is always the first item that gets included twice!

<script src="/assets/utilities.js?body=1" type="text/javascript"></script>
<script src="/assets/utilities.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery-ui.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/autocomplete-rails.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>

更新

作为临时解决方法,我在列表顶部添加了 // = require dummy ,它指向一个空的dummy.js文件。虽然它在渲染页面中出现两次,但它没有任何效果,因为它什么也没做。另一方面,如果我在 development.rb 中设置 config.assets.debug = false ,如通常建议的那样,然后我的所有javascript都被加载两次:

As a temporary workaround, I added //= require dummy at the top of the list, which points to an empty dummy.js file. Although it appears twice in the rendered page, it has no effect, as it does nothing. On the other hand, if I set config.assets.debug = false in development.rb, as often suggested, then ALL of my javascript gets loaded twice:

<script src="/assets/application.js" type="text/javascript"></script>
<script src="/assets/application.js" type="text/javascript"></script>

和js动作被触发两次(例如,在删除模型时弹出两次确认对话框)

and js actions are fired twice (e.g. confirm dialogs pop up twice when deleting a model)

Giuseppe

推荐答案

事实证明,罪魁祸首是插件,特别是,是小部件初始化的一部分。

It turns out that the culprit was the rails-widgets plugin, and in particular this bit of code, part of the widgets' initialization.

取下后,一切恢复正常。 Javascript资产现在按预期加载一次,无论是开发还是生产。

After removing it, all went back to normal. Javascript assets are now loaded once as expected, in both development and production.

虽然我很抱歉没有尽快发现问题对我的设置非常具体,但我仍然希望解决方案可以达到某种目的。谢谢大家。

While I apologize for not finding out sooner that the problem was quite specific to my setup, I still hope that the solution may serve some purpose. Thank you all.

Giuseppe

这篇关于在Rails 3.1基于资产的应用程序中包含两次javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:50