Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using rails 3.2 and i have to set a background for one of the page and i have tried many ways and nothing went right, so looking for some good help. I have tried

background: url(<%= asset_path 'background.jpg' %>)  background: url("public/background.jpg");  background-image:url('/assets/images/background.jpg') 

and nothing worked. Please help me.

like image 783
logesh Avatar asked Aug 07 '13 10:08

logesh


2 Answers

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 
like image 53
Rajarshi Das Avatar answered Sep 27 '22 18:09

Rajarshi Das


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.

like image 43
ernbrn Avatar answered Sep 27 '22 20:09

ernbrn