javascript included twice in a Rails 3.1 asset-based app

∥☆過路亽.° 提交于 2020-01-03 15:56:56

问题


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

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

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>

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>

UPDATE

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>

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

Giuseppe


回答1:


I had faced something similar before and I just left a the first link blank and it worked for me. Give it a try.

/*leave the following line empty */

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



回答2:


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

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




回答3:


Take a look at this:

Rails 3.1 Assets - Strange Serving in Development

You should also check to see that you haven't compiled your assets i.e there is no public/assets directory. If there is delete it, bounce your server and you should be good to go.

Ultimately, both of these solutions didn't work for me so I just set config.assets.debug to false. I can't really debug my js that way, but at least it fixes the problem for now.



来源:https://stackoverflow.com/questions/8558472/javascript-included-twice-in-a-rails-3-1-asset-based-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!