将数据属性与 rails image_tag 和 haml 一起使用

Using data attribute with rails image_tag and haml

我正在尝试使用 Echo.js lazyloader。

为此,我需要使用一个数据属性,该属性将包含要延迟加载的图像的路径,例如:

<img src="/assets/images/placeholder.png" alt="" data-echo="/assets/images/myImage.jpg">

我正在使用 rails 和 haml。根据几个答案 ,我应该将普通的 html 翻译成:(但他们没有指定当数据链接到图像路径时会发生什么)

= image_tag("placeholder.png", :data => { "echo" => "/assets/images/myImage.jpg"})

= image_tag("placeholder.png", "data-echo" => "/assets/images/myImage.jpg")

已显示 placeholder.png 图像,但从未加载 myImage。

使用检查器时,myImage 路径似乎没有被识别为图像路径。

Any ideas? Alternatively if you could suggest a lazy loader that doesn't rely on data- attributes and works with rails and haml, this would be great. Thanks

您还需要在占位符上使用 rails 资产助手

= image_tag("placeholder.png", data: { "echo" => image_path("myImage.jpg") })