10

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.

Community
  • 1
  • 1
sissy
  • 2,908
  • 2
  • 28
  • 54
  • try once asset_url also.....in place of image_url – Rahul Singh Feb 08 '14 at 19:26
  • 1
    hm, i call it like this `image-url('whitey.png')` are you sure that it's `image_url`? – phoet Feb 09 '14 at 02:46
  • config.assets.enabled = true? – Raj Feb 09 '14 at 10:29
  • From rails guides: The asset pipeline is enabled by default. It can be disabled in config/application.rb by putting this line inside the application class definition: `config.assets.enabled = false`, but also tried it. And no success of course :) – sissy Feb 09 '14 at 10:41
  • 1
    Have you tried compiling your asset with `RAILS_ENV=production rake assets:precompile`, I've had similar issues when using `rake assets:precompile` i.e. when using `RAILS_ENV=development`. Though I guess setting `config.assets.digest = true` should enable this... – lwe Feb 18 '14 at 10:35
  • Just to confirm that emaillenin's solution actually works for me, but is really ugly. Using ERB on top of SCSS is ugly, as is copying non-fingerprinted resources or manually adding fingerprints. – zmilojko Nov 11 '14 at 08:38

9 Answers9

4

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.

Raj
  • 22,346
  • 14
  • 99
  • 142
  • are you running in Development or production? asset_path has no effect in development. Check your Rails Env. – Raj Feb 09 '14 at 10:41
  • production env. Usign apache+passenger – sissy Feb 09 '14 at 10:44
  • capistrano is restarting the server at each deploy – sissy Feb 09 '14 at 10:46
  • assets:precompile again? – Raj Feb 09 '14 at 10:46
  • at each deploy capistrano recompiles the assets and restarts the server. that's what it is for :) – sissy Feb 09 '14 at 10:48
  • never used capistrano before :) – Raj Feb 09 '14 at 10:48
  • did u read [this](http://stackoverflow.com/questions/16843143/rails-4-image-path-image-url-and-asset-url-no-longer-work-in-scss-files?rq=1)? – Raj Feb 09 '14 at 10:52
  • yes i did and the last try i did is to move my assets around... they're working if i move them in /public, using the asset-url('image.jpg', image) way. But the problem still remains, cause it's not the compiled version of the asset that the application in using.. – sissy Feb 09 '14 at 11:48
  • try placing the output of `asset_path 'bg.jpg'` in a view script – Raj Feb 09 '14 at 11:51
  • are you getting fingerprint for any of the resources - css, js, fonts etc? or none at all. does it used to work earlier or this is the first time you are using asset pipeline? – Raj Feb 09 '14 at 12:03
  • I'm getting fingerprints for all the resources (js and css). Only image_url helpers (and similar) inside .scss files are not working – sissy Feb 09 '14 at 12:33
  • what is the helper method used to generate js path with fingerprint? – Raj Feb 09 '14 at 12:44
  • no helper methods, sprockets does it by itelf, just including `javascript_include_tag` in my application.html.erb file. This, with assets precompilation, results in digested assets urls – sissy Feb 09 '14 at 12:58
  • 1
    so this answer actually helped me: SCSS preprocessor does not correctly check if the referenced image has been appended a hash, and thus it cannot really be used this way. Adding ERB preprocessor to process SCSS before SCSS preprocessor solves this problem. – zmilojko Nov 17 '14 at 17:28
  • This is a great answer – simo Aug 25 '18 at 14:13
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 the tmp 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
Subhas
  • 14,290
  • 1
  • 29
  • 37
1

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

Community
  • 1
  • 1
Paritosh Piplewar
  • 7,982
  • 5
  • 26
  • 41
1

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
Kris
  • 19,188
  • 9
  • 91
  • 111
0

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')
Nicholas.V
  • 1,926
  • 2
  • 15
  • 30
  • Assets are well precompiled, the problem is the generated path by image_url. Also tried to rename the stylesheet but it has no effect – sissy Feb 18 '14 at 23:30
0

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;
}
TayyabZahid
  • 187
  • 3
  • 15
0

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.

Usman
  • 1
  • images are well precompiled, put in public/assets folder, with a proper digest. the problem is the generated path in the stylesheet: it searches for not-compiled assets in the wrong place – sissy Feb 18 '14 at 23:33
0

I have the same issue here, I fixed it by Base 64 inlining the images using

asset-data-url("images/image.jpg")

Informatom
  • 103
  • 4
0

use image-url in case of .scss as image_url works for .erb files.

krsoni
  • 539
  • 7
  • 11