当我在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?
答案 0 :(得分:10)
<%= image_tag "dark-logo.png", data: { src: "dark-logo.png", src_retina: "logo.png" } %>
在Rails API Docs中提到过,但有点难以找到。
在data
哈希中,任何-
都应该被_
替换,因为Ruby不喜欢-
作为变量名称的一部分。