Providing partial / multi-block layout

I have a very simple requirement - I have a layout consisting of a header and a body. This is a sub-layout of the page, not the page itself.

This layout is repeated across multiple pages and it is possible that the structure around it changes. So I want to be able to decouple the header and body content from the structure that contains it.

My first attempt was to use rendering of a partial as layout, which used named tutorials to render the header and body:

<header class="Resource-header">
  <%= yield :resource_header %>
</header>
<div class="Resource-body">
  <%= yield :resource_body %>
</div>

      

Then draw it from my templates like this:

<%= render layout: 'admin/resource' do %>

  <% content_for :resource_header do %>
  <% end %>

  <% content_for :resource_body do %>
  <% end %>

<% end %>

      

However, this does nothing.

I started playing around with the order of things and found that if the content_for declarations are declared before the partial call, this approach actually works:

<% content_for :resource_header do %>
<% end %>

<% content_for :resource_body do %>
<% end %>

<%= render layout: 'admin/resource' do %><% end %>

      

However, this is just incredibly hacks. It seems to be content_for

defined globally and content_for

there is no relationship between block and partial render.

So what is the correct way for me to achieve this?

+3


source to share


1 answer


Take a look at the rails presenters https://www.ruby-toolbox.com/categories/rails_presenters Perhaps your solution is a gem.



0


source







All Articles