Adding a background image in Ruby on Rails 2 in CSS

前端 未结 6 938
野趣味
野趣味 2020-12-07 19:08

I generated a scaffold in ruby and I want to insert a background image to every page on the site but Im not sure what the link to the image should be.

my image is

相关标签:
6条回答
  • 2020-12-07 19:43

    The webserver in rails takes public folder as the base of the application. Hence its looking for the specific image under /public/images/ instead of app/assets/images/"dep.jpg" & since its not there over there you cannot get the image. Try to put your image in /public/images/ folder then it would work.

    Edit: If you are using rails 3.1 then this would be different as Rails 3.1 uses the concept of asset pipeline for the assets of your application so then then path app/assets/images/dep.jpg would obviously work.

    0 讨论(0)
  • 2020-12-07 19:43

    background: url('/assets/image_name.jpg');

    This worked for me in Rails 4.0

    0 讨论(0)
  • 2020-12-07 19:44

    If your image is in app/assets/images/dep.jpg, then in the css file in rails you should write background: url('/assets/dep.jpg');

    You don't have to put the image in the public folder this way.

    0 讨论(0)
  • 2020-12-07 19:45

    If you are using a modern version of rails (3.1 or later I believe), you can use:

    background: url('../dep.jpg');
    

    Since the css also lives in your assets folder .. automatically refers to the assets folder.

    0 讨论(0)
  • 2020-12-07 19:48

    In Rails 3.1, the path will actually be `/assets/dep.jpg':

    background-image: url(/assets/dep.jpg);
    

    If you convert your scaffold.css file to a Sass file (rename to scaffold.css.scss) then you can use the helper:

    background-image: image-url("dep.jpg");
    
    0 讨论(0)
  • 2020-12-07 19:52

    You can use absolute path for sure:

    background-image:url('/images/dep.jpg');
    
    0 讨论(0)
提交回复
热议问题