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")