ahnbizcad
ahnbizcad

Reputation: 10797

Rails 4 asset or image path from CSS

my file structure is like so

app
  assets
    bookshelf
      assets
        my_image.jpg
    images
      standard_image.png

In my css.scss file, both of these work in development, but they don't work in production.

#some_div {
  background: url("standard_image.png")
}


#some_div {
  background: url("assets/my_image.jpg")
}

I've done RAILS_ENV=production rake assets:clean assets:precompile. I've deleted my tmp/cache.

I feel like this may be a config problem. What am I missing?

I know the images is on the server. I got the digested file name and put it into the url with the production root address.


When I try to use

      background: asset-url("assets/my_image.jpg", image)

I get

Sprockets::Rails::Helper::AssetFilteredError at /books

Asset filtered out and will not be served: add Rails.application.config.assets.precompile += %w( assets/bookshelf_skin02_top_bg.jpg ) to config/initializers/assets.rb and restart your server

Obviously, I don't want to add each asset file in the config.

Upvotes: 2

Views: 6429

Answers (3)

ahnbizcad
ahnbizcad

Reputation: 10797

I created /config/initializers/asset.rb and put the relevant file extensions in regex form.

I believe this works because this tells the asset pipeline to precompile all of these file types in lib or vendor, which aren't done automatically in rails 4.

# Images
Rails.application.config.assets.precompile << /\.(?:png|jpg|jpeg|gif)\z/

# Fonts
Rails.application.config.assets.precompile << /\.(?:svg|eot|woff|ttf)\z/

Upvotes: 5

Magicpulse
Magicpulse

Reputation: 36

Remove assets folder :

background: image-url('my_image.jpg');

Upvotes: 1

user3366155
user3366155

Reputation: 452

Set true in your config/environments/production.rb

config.assets.compile = true

Upvotes: 0

Related Questions