Rails 4.0 image_tag自定义属性

时间:2013-11-06 15:57:01

标签: image ruby-on-rails-4

当我在image_tag中包含可选标记时,会抛出错误:

undefined local variable or method `src' for #<#<Class:0x007fa143339230>:0x007fa147942998>

原始HTML(或所需)

<img src="dark-logo.png" data-src="dark-logo.png" data-src-retina="logo-retina.png" width="244" height="56" alt="">

Rails查看

<%= image_tag "dark-logo.png", data-src => "dark-logo.png %>

如何获取image_tag中的可选标签以呈现所需的HTML?

1 个答案:

答案 0 :(得分:10)

<%= image_tag "dark-logo.png", data: { src: "dark-logo.png", src_retina: "logo.png" } %>

Rails API Docs中提到过,但有点难以找到。

data哈希中,任何-都应该被_替换,因为Ruby不喜欢-作为变量名称的一部分。