Question

I've got a problem with Rails 3.1 assets pipeline. Assets are included twice in development:

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

Rails somehow compiles and includes application.js so all the scripts are included twice - as individual file and in application.js

Everything's fine with precompiled assets in production.

development.rb

 config.assets.compress = false
 config.assets.debug = true

production.rb

# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false

# Compress both stylesheets and JavaScripts
config.assets.compress = true
config.assets.js_compressor  = :uglifier
config.assets.css_compressor = :scss

config.assets.compile = false
config.assets.digest = true

application.rb

config.assets.enabled = true
Was it helpful?

Solution

Try adding the following to development.rb:

config.serve_static_assets = false

...and then clearing your browser cache (update based on comments)

The static assets refer to precompiled assets in public/assets, which is where rake assets:precompile puts them.

What's happening is that anything that exists in public/assets will override anything in app/assets if you are serving them. So public/assets/application.js is being loaded when the js tag is intending to identifiy app/assets/application.js.

OTHER TIPS

Once you get rid of /public/assets, you must also clear browser cache.

This just caused me a problem. Setting the following makes the app work but includes the single application.js file - which I don't want in development:

config.serve_static_assets = false

I had pre-compiled my assets previously (seems to be the cause).

To fix it I did the following:

  • Remove the public/assets dir that the earlier precompilation had added.
  • Run RAILS_ENV=development rake assets:clean to clear out tmp/assets
  • Edited app/assets/application.js

It was only after I edited application.js so it errored and then corrected it that the applciation.js included in the pages was not the full, precompiled application.js.

I'm not sure if all of those need to be done. I was also re-starting my server along the way.

Got tripped up by this (yet again), -- don't forget to add a BLANK LINE after all your //= require directives at the end of your application.js !

I add the same problem with less files.

Here from the documentation:

In development mode, assets are served as separate files in the order they are specified in the manifest file.

My solution was to remove the line *= require_tree . from application.css.less and to only use @import "my-styles"; from less.

Maybe you can find a similar solution with javascript...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top