%div{ :id => "flash_#{flash.keys.first}" }
=h flash[flash.keys.first]
Haml: how to generate dynamic id
-
13-04-2022 - |
题
I've got following code in erb which works fine:
<div id='flash_<%= flash.keys.first.to_s %>'>
<%=h flash[flash.keys.first] %>
</div>
I want to convert it into haml:
#flash_#{flash.keys.first.to_s}
=h flash[flash.keys.first]
But I receive error:
Illegal element: classes and ids must have values.
Which is strange as there IS value, 'flash_' part is always present, I get that error even when I do:
#flash_#{nil.object_id}
Apparently something wrong is with my Ruby interpolation in haml, but I can't get it right. According to documentation http://haml.info/docs/yardoc/file.REFERENCE.html#ruby_interpolation_ #{} is used to interpolate Ruby and it works in such case:
#flash_
#{flash.keys.first.to_s}
but that's not what I want.
To sum up, I want to get the following output:
<div id="flash_foo"> blahblah </div>
but it can be also:
<div id="flash_"></div>
How to obtain that with haml?
解决方案
不隶属于 StackOverflow