4

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?

Darme
  • 6,984
  • 5
  • 37
  • 52
  • Did you run `rake assets:precompile` in development? – Marek Lipka Dec 10 '13 at 10:18
  • Don't think so, but anyway I called `bundle exec rake assets:clean` as you suggested. – Darme Dec 10 '13 at 11:04
  • 1
    @Darmen Don't know if you already solved you problem. If you didn't, did you try to add `config.serve_static_assets = false` to your `development.rb` file ? – Paul D. Dec 19 '13 at 13:40
  • What is the content of `application.js` in development? (Serious question, please post the code here) – Mike Szyndel Dec 19 '13 at 20:19
  • @Paul, maybe `config.serve_static_assets = false` solved the issue, because before (to reply also @Michael) the content of `application.js` in development was, again, all the javascripts included in the manifest, while now, always in development, I see all the calls to each javascript as before but `application.js` includes only the commented lines (from "This is a manifest.." to "GO AFTER THE ..REQUIRES BELOW." and no javascript code anymore. Is this the right behaviour? Anyway now every javascript is actually called just once. Still investigating but the problem seems solved! – Darme Dec 20 '13 at 10:07
  • Anyway, @Paul, please post a comprehensive answer because chances are you found the solution and earned the bounty. – Darme Dec 20 '13 at 10:09
  • Okay, I'll write an answer with some references so you can keep on investigating – Paul D. Dec 20 '13 at 10:11
  • @Darmen : I'm late but still do you have issue? – Neelesh Dec 23 '13 at 09:56

5 Answers5

4

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:

As @gertas said in this post :

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

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.

Community
  • 1
  • 1
Paul D.
  • 2,022
  • 19
  • 19
4

The application.js is not loading twice but the case is -

In development environment assets are not precompiled by default. So your application.js loads at end of all javascripts. Actually application.js file includes all the required js files needed to run the application and if any of them is not loaded before this , it will show you error.

for example My application.js file has this content

//= require jquery
//= require jquery_ujs
//= require foundation
//= require turbolinks
//= require_tree .

Then it means all these js files must be loaded first and then application.js will be loaded.

But in production environment what happens is all the js files are precompiled in single application.js file and you see only one file there.

So, I would suggest not to worry about this. This is normal behaviour of rails 3 application in dev environment.

Ashish Sharma
  • 321
  • 3
  • 13
1

It is not including twice as application.js. Whatever you include in application.js will be referenced in development environment and any JS code that's been written in application.js will be added in last application.js call. Try clicking on application.js reference you will notice this.

In Production environment however; it will only reference application.js that will contain all other referenced file's code as minified.

ducktyped
  • 4,354
  • 4
  • 26
  • 38
  • I know that, I don't get your answer. Application.js works fine, the problem is that, before it and only in development, there is a single call to each javascript file, and I don't know why! – Darme Dec 10 '13 at 11:06
1

Run

bundle exec rake assets:clean

This cleans all precompiled assets, since you don't need them in development environment (they are serveds through assets pipeline).

Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
  • Did it, also specifying the development environment: bundle exec rake assets:clean RAILS_ENV=development Result: /Users/me/.rbenv/versions/1.9.3-p448/bin/ruby /Users/me/.rbenv/versions/1.9.3-p448/bin/rake assets:clean:all RAILS_ENV=development RAILS_GROUPS=assets rm -rf /Users/me/dev/rails/myapp/public/assets But nothing changed – Darme Dec 10 '13 at 11:03
0

Delete Asset Pipeline folder from public folder. It automatically loads the js file from that area by default.

Rubyist
  • 6,486
  • 10
  • 51
  • 86