💎Rails tip: Capture HTML

Sebastien Auriault
Apr 30, 2022
Did you know about the view helper "capture"? It allows you to store HTML into a variable and pass it down somewhere else. You don't necessarily need view_component slots to do that!

<% body = capture do %>
  <div>
    The body of the modal goes here!
    <%= link_to "Confirm", "#" %>
  </div>
<% end %>

<%= render "shared/modal", body: body %>

# shared/_modal.html.erb
<div>
  <div>Header</div>
  <div><%= body %></div>
  <div>Fooder</div>
</div