Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design pattern for side bar with dynamic content in Rails

I would like to have a right side bar with content changes for each page. For example, when I am in Friends page, the side bar should display New Friends. When I am in Account page, the side bar should display Recent Activities.

How should I go about this to respect Rails design patterns? I heard about Cells gem, but I am not sure if I use it.

like image 538
AdamNYC Avatar asked Dec 24 '11 17:12

AdamNYC


1 Answers

here is one way, in your layout add a named yield section

<div id="main-content">
    <%= yield %>
</div>
<div id="side-content">
    <%= yield(:side_bar) %>
</div>

Then in your views put content into the named yield using content_for

# friends view ....
<% content_for(:side_bar) do %>
    <%= render :partial => "shared/new_friends" %>
<% end %>

# account view ....
<% content_for(:side_bar) do %>
    <%= render :partial => "shared/recent_activity" %>
<% end %>    

this requires you to be explicit about what content appears in the side bar for every view, maybe having it do it dynamically is better? probably depends on the specific situation and your preference

see also - http://guides.rubyonrails.org/layouts_and_rendering.html#understanding-yield

like image 132
house9 Avatar answered Sep 27 '22 21:09

house9