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?
In Haml, we write a tag by using the percent sign and then the name of the tag. This works for %strong , %div , %body , %html ; any tag you want. Then, after the name of the tag is = , which tells Haml to evaluate Ruby code to the right and then print out the return value as the contents of the tag.
To use Haml with Rails, simply add Haml to your Gemfile and run bundle . If you'd like to replace Rails's Erb-based generators with Haml, add haml-rails to your Gemfile as well.
%div{ :id => "flash_#{flash.keys.first}" }
=h flash[flash.keys.first]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With