Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a background image in ruby on rails

I am running ruby 2.3.0 and rail 5.0. When trying to display an background image on a view, I use the following CSS class:

.header_img{
   width:100%;
   height: 400px;
   background: url("../../assets/images/home/home-header.jpg");
}

The image is located in the home subfolder under the image assets folder. I have tried trying to find a solution on here but have not been able to find one that works any help would be awesome.

I am using Rubymine as my IDE and nothing seems to work.

like image 269
John Bome Avatar asked Dec 22 '25 01:12

John Bome


2 Answers

There is no need to provide absolute path as your image is in assests so you can directly do something like this

background-image: url('image.png')
like image 141
Divya Sharma Avatar answered Dec 23 '25 18:12

Divya Sharma


You should have a look at the asset-pipe line, see "2.3.1 CSS and ERB":

http://guides.rubyonrails.org/asset_pipeline.html

The asset pipeline automatically evaluates ERB. This means if you add an erb extension to a CSS asset (for example, application.css.erb), then helpers like asset_path are available in your CSS rules:

.class { background-image: url(<%= asset_path 'image.png' %>) }
like image 23
Roger Avatar answered Dec 23 '25 18:12

Roger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!