Jack Carter
Jack Carter

Reputation: 47

cant add background image in rails

I've been trying to add a background image to my rails app but no matter what I try I cant get it to work, this is basic stuff and I don't know what's going wrong. I had it working yesterday but my computer crashed and I hadn't saved my view file after adding the background image.

I've tried:

<body background="bg.png">

as well as:

background-image: url(/assets/bg.png);

and even:

body {
    background: url(bg.png);
}

but nothing will work, what am I doing wrong?

Upvotes: 0

Views: 146

Answers (2)

Kartikey Tanna
Kartikey Tanna

Reputation: 1459

To serve the image from asset-pipeline do the following:

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

To use it inline under .erb files:

<div style="background-image: url('<%= asset_path('some_image.jpg') %>') ">  

What you are trying to do would work if the image was placed under public folder.

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

Upvotes: 1

Rohan
Rohan

Reputation: 2727

If the image is in public directory then

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

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

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

Also, set the below mentioned setting in developement.rb(or other environment on which you are running the code)

config.serve_static_assets = false  
config.assets.compress = true
config.assets.compile = false

Upvotes: 0

Related Questions