Photo by Jalen O’Neal from Pexels

Which came first, the `yield` or the `content_for`?

A little background

<!DOCTYPE html>
<html lang="en">
<head>
<%= yield :react_styles if content_for?(:react_styles) %>
</head>
<body>
<%= render 'layouts/header' %>
<%= yield %>
<%= render 'layouts/footer' %>
</body>
</html>

The problem

The cause

The solution

# layouts/application.html.erb<% content_for(:body_content) { render partial: 'layouts/body_content' } %><!DOCTYPE html>
<html lang="en">
<head>
<%= yield :react_styles if content_for?(:react_styles) %>
</head>
<body>
<%= yield :body_content %>
</body>
</html>
# layouts/_body_content.html.erb<%= render 'layouts/header' %>
<%= yield %>
<%= render 'layouts/footer' %>