Static pages and assets in Rails 3.1.1

久未见 提交于 2019-12-03 08:56:51

Upon further scrutiny of the production.rb I am seeing: "config.serve_static_assets = true" that when set to false by default evokes the issue experienced in webrick. So when setting that to true it serves the files up properly.

From some additional reading it appears that perhaps Heroku needs this set to false as well, which is the environment to which we're deploying.

Thanks for the input, but this appears to be the approach to take for now and I'd certainly appreciate any further input if this is NOT the correct answer or if there's a better approach.

As of Rails 3.1.1 the precompile task creates non-digested as well as digested filenames, so you can refer to these in static files (while still having the digest version in dynamic files).

The only problem is if you use far-future headers on the assets directory; changes to the undigested files won't be pick up by remote clients that still have a copy and believe the cache to still be valid.

You may need to look at an approach the replaces the non-digested filenames with the correct name during the deployment process.

If you do not use far-future headers in the directory then it does not matter - you can use either name.

Aldo 'xoen' Giambelluca

For me the @ylluminate's answer helped: I've changed the config.serve_static_assets option to true in the config/environments/production.rb file and restarted the server with

$rails server --environment=production

and now it serves compressed assets.

NOTE: I've also precompiled the assets with

$bundle exec rake assets:precompile

(call rake this way assure will be used the rake version choosen for the project but I guess use just rake assets:precompile will work 99% of the times)

If you have /public/some_folder/some-image.png physically present (no matter if you just copied it there manually or it was generated by assets precompile), it must work. The server (e.g. Apache) will first check if the requested path exists in public, if it does it won't even call Ruby on Rails.

As far as digested filenames are concerned there is an option to turn this feature off, but I wouldn't recommend that for reasons already mentioned by someone else here.

Also you can put files that refer to assets in the app/assets folder and add a .erb extension AT THE END. Then you can use <%= asset_path ... %> inside that file, so no manual editing will be necessary. This will work even if you already have some other preprocessing on the file, for example sass - style.css.scss.erb will work. First the erb code will be evaluated (putting in the correct filenames for assets) then the sass compiler will be ran.

Oh and have a look at the sprockets-image-compressor gem, just add it to your Gemfile and it will automagically compress image assets too (losslessly using pngcrush and jpegoptim)...I don't know if the gem is rock-solid but from what I've seen I love it!

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