Search code examples
htmlcssruby-on-railsrubybackground-image

How to add background image in Ruby on Rails


I'm trying to put a background image in a ruby app, I've seen that this question was already answered in other posts but it didn't work for me and I would like to know where am i failing.

I have a background called background1.jpg in both '/pubic/images' and in 'projectname/app/assets/images/' folders but it doesn't seem to work.

I've been trying directly from the html.erb for example

<div id="boxy" background-image="......."> </div>

but it didn't work. So i went to the css and its something like this:

boxy{
   ...
   background-image: url('projectname/app/assets/images/background1.jpg');

}

I have also tried using 'public/images/background1.jpg' in the url path of the css

Note: this is not the textual code, its just a representation.

None of this methods has given me a result. Im using Ruby 4.2.0


Solution

  • Try in your css:

    #boxy{
      background: image-url('background1.jpg') no-repeat;
    }
    

    Or directly in your view:

    <%= image_tag('background1.jpg', style: "height: 30px; width: 30px") %>