Rails: How to set a background image in rails from css? Rails: How to set a background image in rails from css? ruby-on-rails ruby-on-rails

Rails: How to set a background image in rails from css?


In your CSS:

background-image: url(background.jpg);

or

background-image: url(/assets/background.jpg);

In environments/production.rb:

# Disable Rails's static asset server (Apache or nginx will already do this)  config.serve_static_assets = false# Compress JavaScripts and CSS  config.assets.compress = true# Don't fallback to assets pipeline if a precompiled asset is missed  config.assets.compile = false# Generate digests for assets URLs  config.assets.digest = true


For sass (scss) this code works with the following image path

app/assets/images/pictureTitle.png

body {   background-image: image-url('pictureTitle.png'); }

You might also need to restart your rails server.


If you have the image in your public directory like public/bg.jpg

background-image: url('/bg.jpg')

If you have image in app/assets/images/bg.jpg

 background-image: url('/assets/bg.jpg')