1 year ago
#76856
BEEK
Using layout with content_for/yield when rendering collection partial
I'm trying to reuse a card layout throughout my app for various collections. I was hoping I could do this with content_for and yield, as described in the rendering guide, and my exact use case is described here. But I'm not able to get the content from the partial inserted into the layout. Here's the setup for rendering a collection of 'entities':
The view (index.html.erb):
<%= render partial: 'entity', collection: @entities, layout: 'card' %>
The partial (_entity.html.erb):
<% content_for(:left_icon) do %>
<i class="fas fa-city fa-2x"></i>
<% end %>
The card layout (_card.html.erb) uses yield to retrieve content specific to the collection (like a certain left-icon as in this snippet)
<div class="card">
<div class="card-body d-flex">
<%= yield :left_icon %>
</div>
</div>
But only an empty line is rendered where the icon should be:
<div class="card">
<div class="card-body d-flex">
</div>
</div>
What do I need to do to be able to render the card with content?
ruby-on-rails
collections
view
partial
content-for
0 Answers
Your Answer