Dynamic Content Sidebar Design Pattern in Rails

I would like to have a right panel with content changes for each page. For example, when I'm in the friends page, new friends should appear in the sidebar. When I'm on the Accounts page, the sidebar should display Recent Activity.

How do I do this to respect Rails design patterns? I heard about the Cells jewel, but I'm not sure if I use it.

+4
source share
3 answers

here is one way in your layout to add a section called yield

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

Then in your views put the content in a named resource 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 that you clearly indicate what content is displayed in the sidebar for each view, perhaps if it is done dynamically better? probably depends on the specific situation and your preferences

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

+8
source

I came to this question at the time of a big design change in our views. After thinking a bit about the sidebar issue, I realized that there is no better solution (as always). There are better solutions for every occasion.

I will compare solutions 3 :

  • using content_for (: sidebar) and yield (: sidebar)
  • using partials approach
  • using a cell harness

1. Using content_for(:sidebar) and yield(:sidebar)

This is good for cases where each link (every controller action) that you access creates a different sidebar. In this case, each view access will have a content_for(:sidebar) .

If your presentation of the sidebar depends only on the state of any variable in the session, for example, the sidebar should not be displayed for every link you access. Then you should use a good caching system like turbolinks to avoid repeating the same thing multiple times, or use something like javascript Cell Cells to display only the main part of the layout.

2. Using partials

Using partial sentences is always useful for eliminating duplication. If your sidebar is very simple and modified for each controller, you can display it as partial. But if you process different partial elements in one controller, depending on some state, this may be a sign that your views have business logic, which should be avoided.

3. Using Gemstone Cells

A very good design template when you have to display the sidebar each time from a different controller than the rest of the view.

A lot of business logic is required from the view, which is certainly good practice.

Here you have an action that triggers a view . Inside this view there is a render_cell(:sidebar, params) operator render_cell(:sidebar, params) . This statement will execute some business logic and display a sidebar view. It is as if the first action called other controller actions to display certain parts of your view (called cells)

If you only make changes to the sidebar, you may need to create another simple action for javascript to request it. This action will again call the render_cell(:sidebar) method to respond to this view.

This is a very interesting approach.


Other ideas:

  • Your sidebar can only be displayed using javascript from the same action.
  • Your sidebar can be displayed by an angular controller, and the rails send jsons with sidebar objects. (find "Single Page Applications")
+5
source

try something like this

 <div class="sidebar"> <% if current_page?(controller => "friends", :action => "show") %> <h4>New Friends</h4> <% elseif current_page?(controller => "accounts", :action => "show") %> <h4>Recent Activities</h4> <% end %> </div> 

If the above code matches what you are trying to do (it seems like this is what you want to achieve) then stick with it, otherwise it might be useful to go with some gems. Also check out the helper page on how to use the current page? method. Hope this helps.

+2
source

Source: https://habr.com/ru/post/1387887/


All Articles