When I generate a default scaffold, the display tags on show.html.erb have
<%=h @broker.name %>
I know the difference between <%
and <%=
. What's the "h" do?
When I generate a default scaffold, the display tags on show.html.erb have
<%=h @broker.name %>
I know the difference between <%
and <%=
. What's the "h" do?
html escape. It's a method that converts things like < and > into numerical character references so that rendering won't break your html.
<%=h is actually 2 things happening. You're opening an erb tag (<%=) and calling the Rails method 'h' to escape all symbols.
These two calls are equivalent:
<%=h person.first_name %> <%= h(person.first_name) %>
The "h" method is commonly used to escape HTML and Javascript from user-input forms.
It's worth noting that h is a method alias for html_escape from the ERB::Util class and you can find ERB API docs here:
There is also a method in Rack to escape HTML Rack::Utils.escape_html
in case you are in Metal and want to escape some HTML.