36

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
lucapette
  • 20,564
  • 6
  • 65
  • 59
Fenelon
  • 700
  • 2
  • 6
  • 16
  • 3
    try `rake assets:clean`. development might still serve application.js but it shouldn't have all the other js files in it. – firien Dec 03 '11 at 01:21
  • Don't forget to clear the browser cache after removing the assets in `public/assets`. – Nicolas Goy Sep 30 '12 at 10:17

5 Answers5

39

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.

Ethan McCutchen
  • 514
  • 4
  • 5
  • It still adds `application.js`, but it's now empty. Thanks! – Fenelon Dec 05 '11 at 19:40
  • 18
    Seeing this same problem running on `rails 3.2` and this fix doesn't work for me. There's also nothing in `public/assets` as this is a brand new project, and I haven't run `rake assets:precompile`. Any idea's how to fix it? I have to run with `config.assets.debug = false` in development mode just to get my javascript to function properly and it's getting annoying! – Batkins Feb 23 '12 at 19:51
  • Hmm interesting... this solution seems to work the app, but only after I ran `rake assets:precompile` for the first time. Makes you wonder why it's ignoring the `config.assets.compile = false` line. – Batkins Feb 23 '12 at 20:01
  • I'm having the same issue as @Batkins in a Rails 3.2 app. When `config.assets.debug = true` the application.js contains the packed concatenated version. If I turn debug off this goes away and application.js just contains all of the assets together (but unpacked and readable). – Ben Scheirman Mar 06 '12 at 14:25
  • 6
    @BenScheirman - I have reported [this as a bug to the rails github issues tracker](https://github.com/rails/rails/issues/5145). Someone has submitted a pull request to patch it but it hasn't been accepted yet. Hopefully it will soon. Feel free to give it a +1. – Batkins Mar 06 '12 at 15:56
  • Rails3.2 users please notice: after setting `config.assets.compile = false`, you have to also run `bundle exec rake assets:precompile`. then it will work. – Siwei Mar 15 '12 at 01:01
  • 2
    I faced the same problem as @Batkins (Rails 3.2), and it turned out that the browser cache was the problem (as I'm new to mac, I wasn't using command + shift + R) – yorch Jul 11 '12 at 01:22
  • This setting actually refers to all of `public/` and pre-dates the assets pipeline. http://guides.rubyonrails.org/configuring.html – graywh May 03 '13 at 20:30
12

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

Effisfor
  • 183
  • 1
  • 7
8

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.

Peter
  • 417
  • 4
  • 15
  • I think the edit to `app/assets/application.js` is required to force the browser to reload it (gets a 304 otherwise). You may also just clear the browser cache after removing `public/assets`. – Nicolas Goy Sep 30 '12 at 10:16
  • This specific solution worked for me. Thank you. The asset pipeline is cool but it sure is fragile. Far from turn-key but we'll get there. – Matt Schwartz Nov 14 '12 at 00:08
  • Editing the application.js was the step that I was missing - thanks! – benz001 Mar 19 '13 at 04:32
4

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 !

tw airball
  • 1,359
  • 11
  • 12
  • I just recently has to re-setup my development environment after a hard drive crash and had this issue. For some reason adding another new line at the end of the last directive fixed it. – zarazan May 22 '13 at 21:52
  • My application.js already had a blank line, but similar to @zarazan, above, adding a second blank line at the end fixed it. I cannot yet determine why. – Morris Singer Aug 02 '13 at 20:06
  • This is crazy. I added the white line and the issue got fixed. I removed the line... and now it continues to work...... – richardaday Oct 30 '13 at 15:34
0

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...

Charles
  • 11,367
  • 10
  • 77
  • 114