When I include the optional tags within image_tag it throws an error:
undefined local variable or method `src' for #<#<Class:0x007fa143339230>:0x007fa147942998>
Original HTML (or desired)
<img src="dark-logo.png" data-src="dark-logo.png" data-src-retina="logo-retina.png" width="244" height="56" alt="">
Rails View
<%= image_tag "dark-logo.png", data-src => "dark-logo.png %>
How do I get the optional Tags in image_tag to render the desired HTML?
<%= image_tag "dark-logo.png", data: { src: "dark-logo.png", src_retina: "logo.png" } %>
It's mentioned in the Rails API Docs, but a bit hard to find.
Inside the data
hash, any -
should be replaced by a _
, since Ruby doesn't like a -
as part of a variable name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With