28

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 in app/assets/images/"dep.jpg"

this is what i have but it isnt working:

background-image:url('../images/dep.jpg');
}

Any help? thanks

Choylton B. Higginbottom
  • 2,236
  • 3
  • 24
  • 34
Dan
  • 8,263
  • 16
  • 51
  • 53
  • try giving the exact path. Coz even the folder `/public` has `images` folder and I think its looking there instead of your path – uday Jan 26 '12 at 19:48
  • It depends on where your file is, relative to your images directory. `../` says go one directory up from the current file location and look for an images directory. What does your directory structure look like? – Matt K Jan 26 '12 at 19:49
  • my rails project folder is called ClassSchedule/apps/assets/images/dep.jpg – Dan Jan 26 '12 at 19:52
  • 1
    and where is the file containing this line: `background-image:url('../images/dep.jpg');` – Matt K Jan 26 '12 at 19:53
  • in my css file located at ClassSchedule/apps/assets/stylesheets/scaffold.css – Dan Jan 26 '12 at 20:05

6 Answers6

77

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");
Dylan Markow
  • 123,080
  • 26
  • 284
  • 201
19

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.

uday
  • 8,544
  • 4
  • 30
  • 54
  • im kind of new to rails so im not sure where the public folder is. im just running this on localhost using rails server – Dan Jan 26 '12 at 19:57
  • `public` folder is present in root folder of your `rails` application. The hierarchical structure goes like `/public/images` – uday Jan 26 '12 at 19:59
  • my public folder doesnt have an images folder, should I make one or should I just put it in the public folder? – Dan Jan 26 '12 at 20:04
  • make a folder `images` & put it there so that you can have all your `image-type` files at a place. – uday Jan 26 '12 at 20:05
  • it has to do with CSS. you have to set an attribute to `no-repeat` like that. & Please dont forget to rate my answer if it helped. – uday Jan 26 '12 at 20:11
  • This completely bypasses Rails 3.1's new asset handling features... It works, but it requires you to keep your image in the non-default location. – Dylan Markow Jan 26 '12 at 20:14
11

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.

2

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

This worked for me in Rails 4.0

laffan
  • 319
  • 3
  • 8
1

You can use absolute path for sure:

background-image:url('/images/dep.jpg');
Oleksandr Skrypnyk
  • 2,762
  • 1
  • 20
  • 25
  • I was wondering since Rails takes `public` as the basefolder, dont you think even if he does apply the absoulte path it wont work unless & until the file is reachead by the webserver by some strange tweak? – uday Jan 26 '12 at 20:14
0

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.

Wylliam Judd
  • 9,935
  • 2
  • 26
  • 36