Mason Mason - 3 months ago 12
CSS Question

Reference rails image in jquery function

I have a jquery function that's supposed to change the background of the page. I want the background to be an image in the app/assets/images directory. However, I'm confused as to the proper way to reference the image with the asset pipeline.

Here's the line I have now:

$('body').css("background-image", "url('welcome.png')");


What's the best way to access the image in the url parameter?

Answer

Try this:

$('body').css("background-image", "url('/assets/welcome.png')");

In order to access assets in the pipeline, you have to specify the assets folder.

I've been trying to find where I read this, but Rails basically converts all asset helper urls to something like /assets/asset.png or like /assets/stylesheet.css so you should be able to do the same by just specifying the assets folder in your source url.