Rails is great at generating HTML using helpers such as content_tag and tag.
content_tag(:div, , class: "home")
<div class="home">
</div>
Rails 5.1 has introduced
new syntax for this in the form of
enhanced tag helper.
Now that same HTML div tag can be generated as follows.
tag.div class: 'home'
<div class="home">
</div>
Earlier, the tag type was decided by the positional argument to the
content_tag and tag methods but now we can just call the required tag type
on the tag method itself.
We can pass the tag body and attributes in the block format as well.
<%= tag.div class: 'home' do %>
Welcome to Home!
<% end %>
<div class="home">
Welcome to Home!
</div>
The new tag helper is also HTML 5 compliant by default, such that it respects
HTML5 features such as
void elements.
The old syntax of content_tag and tag methods is still supported but might
be deprecated and removed in future versions of Rails.

Related blogs
- 01Rails 5.1 stops loading all records on Model.all#inspectNovember 14, 2017 · Mohit Natoo
- 02Rails 5.1 enhances ActiveSupport::TimeZone.country_zonesJuly 18, 2017 · Narendra Rajput
- 03Rails 5.1 dropped jQuery dependency in default stackJune 20, 2017 · Neeraj Singh
- 04Rails 5.1 adds delegate_missing_toMay 30, 2017 · Neeraj Singh