Rails 3.1 Assets - Strange Serving in Development

时光毁灭记忆、已成空白 提交于 2019-12-17 10:18:48

问题


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

回答1:


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.




回答2:


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




回答3:


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.




回答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 !




回答5:


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



来源:https://stackoverflow.com/questions/8356251/rails-3-1-assets-strange-serving-in-development

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!