DogEatDog
DogEatDog

Reputation: 3047

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?

Upvotes: 4

Views: 5361

Answers (1)

zwippie
zwippie

Reputation: 15525

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

Upvotes: 10

Related Questions