Skip to content

laying out sections using content_for

For the new style changes for confabulus, I made use of the content_for capabilities in rails. The currently has two sections, main and sidebar. content_for makes implementing this quite easy. In your layout file (example application.html.erb under app/views/layouts), you use a <%= yield :sectionname %> to place each of the sctions. Here I used <%= yield :sidebar %> to include my sidebar section. :sidebar is just the name I chose for the section.

<div class="right" id="main_right">
<div id="sidebar">
<%= yield :sidebar%>
</div>
</div>

Then in the view for the page, you need to define the content for each section, for sidebar I have:

<% content_for :sidebar do %>
<h2>Please Login</h2>
<div class="content">
... full view code omitted ...
<% end %>

So my views are of the form of:

<% content_for :sidebar do %>
... sidebar content goes here ...
<% end %>
<% content_for :main do %>
... main content goes here ...
<% end %>

You can do this for an arbitrary number of sections.
One thing to note is that if you are converting and forget to put a content_for block around the code, your view will render empty.