问题
I can't understand why my css file is not appending digests to my assets with the helper method image_url
My assets are correctly precomiled, and files do contain the digest. I also can access them (with the digested url) manually. And most strange thing is that in the beginning it was working.
here's my configs:
config.assets.js_compressor = :uglifier
config.assets.compile = false
config.assets.digest = true
config.assets.version = '1.0'
config.serve_static_assets = false #also tried true
here's my application.css: *= require_tree .
here's the common.scss file, used for including an image:
body{
background: image_url('bg.jpg');
font-family: 'Lato', sans-serif;
overflow-x: hidden;
}
The images, as well as the stylesheets are in a subfolder inside assets/images and assets/stylesheets.
here my gems:
gem 'rails', '4.0.0'
gem 'sass-rails', '~> 4.0.0'
I'm deploying with capistrano, but I don't think this is a capistrano related problem, assets are well compiled.
EDIT What i've (unsuccessfully) tried until now:
image-url('image.jpg'); -> http://www.mydomain.it/images/image.jpg
image_url('image.jpg'); -> same as above
url(image-path('header.jpg')); -> http://www.mydomain.it/images/image.jpg
asset-url('image.jpg', image); -> http://www.mydomain.it/image.jpg
problem still remains: assets are compiled but requested without digest.
EDIT
Following this question Rails 4 image-path, image-url and asset-url no longer work in SCSS files I moved around my assets and using the combination of asset-url and putting my assets in /public folder, background images are working, even though the problem still remains as the application is not using the timestamped version of the images. So only a (not that good, nor that bad) workaround.
回答1:
Should use asset_path
. Also, it needs to run under ERB tag, as SCSS does not compile asset_path. Rename common.scss
to common.scss.erb
.body { background-image: url(<%= asset_path 'bg.jpg' %>) }
Read more here.
回答2:
Some things to consider:
- Make sure the image is not "gitignored" or something
- Make sure the same image is not under public folder
- Make sure asset-related gems are directly in the Gemfile, and not under
group: :production
or something. This is the new rails norm - Run
rake assets:clobber
and remove thetmp
folder which contains sass cache - Run
RAILS_ENV=production rake assets:precompile
manually under production environment, and let us know the list of files generated under public - Run
rake assets:precompile
in Development mode, it could provide more logs
回答3:
change your setting from this
config.serve_static_assets = false #also tried true
to
config.serve_static_assets = true
Reason: you are now allowing your assets to be called/served from your app. Earlier you aren't, it is currently served directly by your server i.e apache
or ngnix
.
I don't believe how this is possible
image-url('image.jpg'); -> http://www.mydomain.it/images/image.jpg
image_url('image.jpg'); -> same as above
url(image-path('header.jpg')); -> http://www.mydomain.it/images/image.jpg
asset-url('image.jpg', image); -> http://www.mydomain.it/image.jpg
It should contain assets
in place of images
. if this is the case, there is something else going on apart from asset pipeline.
Since last time my question wasn't helpful so i thought to show you working app, i have created the heroku app (code is hosting on github). You can check all the commits. it is specially done for this question. The final fix is this.
Final fix: https://github.com/passion8/so2_fix/commit/64acc654d8926098ec5a40579f4a0e005037e381
Heroku URL: https://vast-plains-3919.herokuapp.com/
Github code: https://github.com/passion8/so2_fix
Github commits: https://github.com/passion8/so2_fix/commits/master
If this still doesn't work, make a look on
https://stackoverflow.com/a/25511960/1377943
回答4:
Either change the filename from .css
to .css.erb
and use asset_url
:
background-image: url(<%= asset_url('x-icon.png') %>);
Or you can use scss and do
background-image: url(asset-path('x-icon.png'));
Do not include "assets" in the path, i..e not "assets/x-icon.png"
, this does not work.
To test this locally ensure your assets configuration mimics that of production, e.g.
config.public_file_server.enabled = true
config.assets.compile = true
config.assets.digest = true
Or if you start in production environment ensure config.public_file_server.enabled = true
so the public
directory is served by Rails, usually it would be served by Apache or similar.
- Rails 5.1
回答5:
Try changing common.scss
to common.css.scss
Read more: http://guides.rubyonrails.org/asset_pipeline.html#preprocessing
Also, to add additional folders to asset pipeline add the following to config/environments/production.rb
config.assets.paths << Rails.root.join('app', 'assets', 'images')
config.assets.paths << Rails.root.join('app', 'assets', 'stylesheets')
回答6:
I am using this and its work for me. Use file with this format common.css.scss
.body
{
background:url(image_name.gif) no-repeat;
}
回答7:
rails doesn't attach a digest for the assets that are not actually present so make sure that the images are already there in image directory.
回答8:
I have the same issue here, I fixed it by Base 64 inlining the images using
asset-data-url("images/image.jpg")
回答9:
use image-url
in case of .scss as image_url
works for .erb files.
来源:https://stackoverflow.com/questions/21650699/rails4-image-url-not-generating-digest-in-scss