Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4.0 image_tag custom attributes

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?

like image 948
DogEatDog Avatar asked Dec 25 '22 18:12

DogEatDog


1 Answers

<%= 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.

like image 123
zwippie Avatar answered Jan 10 '23 05:01

zwippie