Lightweight Proc Partials in Rails
I recently needed to display some content “cards” from dynamic content interspersed with static content on a Rails-backed website I was building for a client. Essentially, I needed something like this…
<div class="container">
<div class="person">John Smith</div>
<div class="person">Bill Nye</div>
<div class="interstitial">
<a href="/sign-up">Sign Up Now</a>
</div>
<div class="person">Carl Sagan</div>
</div>
…only where the people are filled in from the database. One solution is using CSS ordering instead of source ordering. This can work really well, but it takes additional time and can increase the cognitive overhead for reasoning about the behavior of a piece of code, especially for someone new to the project.
I wanted the source order to reflect the display order, but I didn’t want to repeat myself unnecessarily.
Verbose, and liable to trip me up in the future:
...