1

I'm just adapting template code to a Rails web app.

I've got inline CSS referencing image assets like this:

  <div class="head" style="background-image: url('../somepath/b43.jpg');">

I'd like to store and reference images in the asset pipeline. I've used helpers like image_path before, but only in Sass files.

How do I write code that looks something like:

  <div class="head" style="background-image: url(image_path('b43.jpg'));">

that the inline CSS (using Rails asset helper) gets emitted with actual image asset folder paths when the page is rendered?

Right now, I'm just getting:

  <div class="head" style="background-image: url(image_path('b43.jpg'));">

in the HTML.

1 Answer 1

1

You must use inline erb style for this

<div class="head" 
     style="background-image: url(<%= image_path('b43.jpg') %>);">
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Iceman. I did this and got: <div class="head" style="background-image: url:('/images/b32.jpg');">. I need to reference /assets/background/b32-somefingerprint.jpg. My other image_tag helpers in my ERB correctly reference the asset path (e.g. image_path "avatar1.jpg" yields /assets/avatars/avatar1-somefingerprint.jpg or image_path "b1.jpg" yields /assets/blog/b1.jpg). What am I missing?
Well, the asset helper should automatically reference the correct file.
You say you need /assets/background, if the image is in the background folder you'll need to do image_path('background/b32.jpg')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.