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.
In your CSS:
or
In
environments/production.rb
:For sass (scss) this code works with the following image path
app/assets/images/pictureTitle.png
You might also need to restart your rails server.
Try the code below, it will definitely work:
The problem could be more deeply ingrained than you think. It most likely is not a Rails asset problem as many presume, but 'miscommunication' between your html elements. Here's why:
First of all, fool proof your code by puting the backgound image in the
body
element.Once you realize that the console doesn't give a 404 anymore, confirm the image actually loads with this:
http://localhost:3000/assets/pic-name.jpg
body
element is hiding something - when you put the image in thebody
, it works, when you put it in another element, it works not.This is the trick; in that
other
element where you want the background image, mine washeader
, insert some text or some lines, Yes, just plain text or something! Mine was:And alas, it worked! Though it didn't show the full image, just the upper part. But at least I knew it worked.
Hope this helps someone. And along with this I realised that it was not that easy to place the background image to cover the
nav
as well, esp if using bootstrap; both thenav
and your other element need to bechildren
of the sameparent element
, eg, mine was theheader
as shown above, and you'll also have to render the nav inside your, say,homepage.html.erb
, and every other page, as opposed to just rendering it on theapplication.html.erb
Update
Okay, this is what I did to show the full background image without inserting texts here and there. In my
application.scss
, where you have your css, I simply added the height property, like soN.B: Using
height: 100%
didn't work.if you have your image into app/assets/images and its name is 'zi-fullscreen-bg.png', for example then you can use
at least it worked for me!
If you have the image in your public directory like public/bg.jpg
If you have image in app/assets/images/bg.jpg