A pretty way to unescape HTML in a Ruby on Rails application
I once sent a Pull Request to our code base here at Zygo containing an .erb tag with an unfamiliar alias. It was the <%== (...) %>
tag alias. I received a request changes on my PR asking if it wasn't a typo. A few months later, another coworker also thought it was a typo and removed the second equals, just to find out it broke our code.
It happens that is very hard to find any internet material, except for comments in already existing Stack Overflow questions, that explains that this, actually, is an alias for the .html_safe
command, used to unescape HTML code inside an .erb file.
@custom_page_title = “Page <strong>Title</strong>”
…
<div>
<h1><%== @custom_page_title %></h1>
</div>
Another very common way to achieve the same goal is calling the raw method, as in <%= raw(...) %>
, but both .html_safe
and raw(...)
make your code look like a careless beach: ugly to see and painful to walk through
Joining the lack of documentation about this alias with the unfamiliarity of our engineering team with it, we thought that maybe there are others Ruby on Rails developers missing out on this information.