Yalın Semer Yalın Semer - 11 days ago 6
Ruby Question

How to reference image in data attribute within Rails 5

i'm new on Rails. I can use images with

image_tag
or
image_path
.

But , i have an html template and these methods does not work in data-attributes. In my html , there is a
data-background
attribute which changes background of the
div
.

My html :

<section id="sub-header" class="section bg-parallax pt-0 pb-0" data-background="<%= image_path('bg/about.jpg') %>">


This directory is under the
app/assets/images/bg
. But i can't reach the image.

Where should i put these images ? and how can i reach images in this example

Answer

I tried all combinations. But solution was simple. You don't need to use rails helper functions for that. I moved my images from assets to public folder and :

  <section id="sub-header" class="section bg-parallax pt-0 pb-0" style='background:url("/bg/about.jpg")'>