最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript included twice in a Rails 3.1 asset-based app - Stack Overflow

programmeradmin4浏览0评论

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 autoplete-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/autoplete-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 autoplete-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/autoplete-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

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 autoplete-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/autoplete-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 autoplete-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/autoplete-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

Share Improve this question edited Jan 6, 2012 at 8:23 Giuseppe asked Dec 19, 2011 at 7:51 GiuseppeGiuseppe 5,3485 gold badges42 silver badges38 bronze badges 4
  • 1 do you have files in public/assets by any chance? if you do delete them as this could be causing this duplication – Hishalv Commented Dec 19, 2011 at 20:13
  • No, there's no assets directory in my development's /public – Giuseppe Commented Dec 20, 2011 at 7:39
  • Have you tried changing config/environments/development.rb config.assets.debug to false, wondering if it does the 'the right thing' when it concats all of the assets – house9 Commented Dec 21, 2011 at 5:39
  • I did try, and it wreaked havoc. Please see the update to the original question. Thanks – Giuseppe Commented Jan 6, 2012 at 8:24
Add a ment  | 

3 Answers 3

Reset to default 13

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 autoplete-rails
//= require utilities

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

Take a look at this:

Rails 3.1 Assets - Strange Serving in Development

You should also check to see that you haven't piled 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.

发布评论

评论列表(0)

  1. 暂无评论