Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript_include_tag :application loads javascripts twice in development environment

This is weird...

This line of code in the head section of my layout:

<%= javascript_include_tag :application %>

Results in this html:

<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/jquery.prettyPhoto.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery.qtip.min.js?body=1" type="text/javascript"></script>
<script src="/assets/dataTables/jquery.dataTables.js?body=1" type="text/javascript"></script>
<script src="/assets/datatable-enables.js?body=1" type="text/javascript"></script>
<script src="/assets/modernizr-1.7.min.js?body=1" type="text/javascript"></script>
<script src="/assets/qtips.js?body=1" type="text/javascript"></script>
<script src="/assets/pagination.js?body=1" type="text/javascript"></script>
<script src="/assets/payments.js?body=1" type="text/javascript"></script>
<script src="/assets/replies.js?body=1" type="text/javascript"></script>
<script src="/assets/searches.js?body=1" type="text/javascript"></script>
<script src="/assets/static_pages.js?body=1" type="text/javascript"></script>
<script src="/assets/user.js?body=1" type="text/javascript"></script>
<script src="/assets/gmaps4rails/gmaps4rails.base.js?body=1" type="text/javascript"></script>
<script src="/assets/gmaps4rails/gmaps4rails.bing.js?body=1" type="text/javascript"></script>
<script src="/assets/gmaps4rails/gmaps4rails.googlemaps.js?body=1" type="text/javascript"></script>
<script src="/assets/gmaps4rails/gmaps4rails.mapquest.js?body=1" type="text/javascript"></script>
<script src="/assets/gmaps4rails/gmaps4rails.openlayers.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery.rateit.min.js?body=1" type="text/javascript"></script>
<script src="/assets/feedbacks.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>

As you can see it generates a call for each javascript resource and a call to the compiled applications.js, which of course includes, again, every javascript resource.

As a result of this, every javascript is called twice!

This happens only in the development environment, while in the production environment the generated html is, accurately, just:

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

And this is my application.js:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require jquery.prettyPhoto
//= require jquery.qtip.min
//= require dataTables/jquery.dataTables
//= require datatable-enables
//= require modernizr-1.7.min
//= require qtips
//= require pagination
//= require payments  
//= require replies
//= require searches
//= require static_pages
//= require user
//= require gmaps4rails/gmaps4rails.base 
//= require gmaps4rails/gmaps4rails.bing 
//= require gmaps4rails/gmaps4rails.googlemaps
//= require gmaps4rails/gmaps4rails.mapquest
//= require gmaps4rails/gmaps4rails.openlayers
//= require jquery.rateit.min
//= require feedbacks

I'm riding Rails 3.2.13, what's going on here?

like image 281
Darme Avatar asked Dec 25 '22 16:12

Darme


1 Answers

I had the same kind of problem before with my Rails application. Including the line

config.serve_static_assets = false

in the development.rb file solved this issue. (This configuration item defaults to true in development according to rubyonrails.org, and to false in production, which is why you were not having this problem in production.)

This setting decides whether Rails should serve the static asserts in the public/ directory. In production, a webserver will handle this task, thus the setting defaults to false.

You may want to take a look at these posts:

  • Rails 3.1 remote requests submitting twice

As @gertas said in this post :

Adding config.serve_static_assets = false to development.rb will prevent loading files from /public/assets.

  • Ruby On Rails 3.1 - assets pipeline - assets rendered twice

And finally, you could also use the configuration guide for Rails at http://guides.rubyonrails.org/configuring.html

It provides a comprehensive explanation about configuration items in a Rails app.

like image 124
Paul D. Avatar answered Dec 28 '22 05:12

Paul D.