Jonathan Bennett

Using Layouts for Better Partials

An additional way to reuse partials is to render them within a wrapper layout.

For instance, if you have a normal partial for a model, you can wrap it with an admin partial that adds edit or delete links:

<!-- _item.html.erb -->
<div id="<%= dom_id(item) %>">
	<%= link_to item.name, item %>
</div>

<!-- _admin_wrapper.html.erb -->
<div class="flex justify-between">
	<%= yield %>
	
	<%= button_to "Delete", item, methed: :delete %>
</div>

<!-- usage -->
<%= render partial: "items/item", collection: @items, layout: "items/admin_wrapper" %>

By using a layout, your item partial remains clutter free and reusable throughout your system.