Javascript included twice in asset-based Rails 3.1 application

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

In short, the first element in the js manifest is included twice.

Here is my whole /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 is a fragment of the provided page 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> 

Note that if I go to the top of any other line in application.js , for example:

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

This is always the first element to be turned on 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 indicates an empty dummy.js file. Although it appears twice on the displayed page, it does not work because it does nothing. On the other hand, if I set config.assets.debug = false in development.rb , as is often suggested, then ALL of my javascript is loaded twice:

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

and js work twice (for example, when deleting a model, confirmation dialogs appear twice)

Giuseppe

+4
source share
3 answers

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

After its removal, everything became normal. Javascript assets are now loaded once, as expected, both in development and in production.

While I apologize for not having previously figured out that the problem was very specific to my installation, I still hope that the solution can serve a specific purpose. Thanks to everyone.

Giuseppe

+3
source

I came across something similar before and I just left 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 
+13
source

Take a look at this:

Rails 3.1 Assets - strange service in development

You should also check that you have not compiled your assets, i.e. no public / assets directory. If there is a deletion, discard your server, and you should be good to go.

Ultimately, both of these solutions did not work for me, so I just set config.assets.debug to false. I cannot debug my js this way, but at least it fixes the problem for now.

+1
source

Source: https://habr.com/ru/post/1387000/


All Articles