# Layouts and partials

**URL:** <https://www.ruby-forum.com/t/layouts-and-partials/136166>\
**Category:** Rails\
**Created:** [April 29, 2008, 3:59pm UTC](https://www.ruby-forum.com/t/layouts-and-partials/136166 "2008-04-29T15:59:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![predhme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e8c25b/32.png) [@predhme](https://www.ruby-forum.com/u/predhme)\
**Post date:** [April 29, 2008, 3:59pm UTC](https://www.ruby-forum.com/t/layouts-and-partials/136166/1 "2008-04-29T15:59:35Z")

</div>

I am currently working on re factoring my controllers and views to  
better follow the DRY principal. I have done significant work with the  
controllers but am struggling with the views…

I have the standard type layout, a header (includes a navigation bar),  
then the content, followed by a footer.

In the content section there is a title, basically the action name,  
and available actions followed by some dynamic content. The actions  
contain a create, list, and search button and other action specific  
actions. Right now those actions are defined in each page. I am trying  
to factor that out and create an action partial which will be shared  
amongst most of my controllers. How can I define a “default” list of  
actions and then when needed override those defaults?

---

<div class="post-metadata">

**Author:** ![predhme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e8c25b/32.png) [@predhme](https://www.ruby-forum.com/u/predhme)\
**Post date:** [April 29, 2008, 4:32pm UTC](https://www.ruby-forum.com/t/layouts-and-partials/136166/2 "2008-04-29T16:32:16Z")

</div>

one approach would be using content\_for.  
roughly in your layout (or wherever you want) you define a yield

\<%= yield :action\_menu %\>

and in the views you would then declare content for that:  
\<% content\_for :action\_menu %\>  
\<%= render :partial =\> “basic\_menu” %\>  
\<% end %\>

in another one:  
\<% content\_for :action\_menu %\>  
\<%= render :partial =\> “basic\_menu” %\>  
\<%= link\_to “another option”, …path to option… %\>  
\<% end %\>

or create them in whatever way you like, mixing partials with defaults  
and inplace code.

> **[ActionView::Helpers::CaptureHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#M001069)**
>
> CaptureHelper exposes methods to let you extract generated markup which can be used in other parts of a template or layout file.

---

<div class="post-metadata">

**Author:** ![predhme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e8c25b/32.png) [@predhme](https://www.ruby-forum.com/u/predhme)\
**Post date:** [April 29, 2008, 7:13pm UTC](https://www.ruby-forum.com/t/layouts-and-partials/136166/3 "2008-04-29T19:13:46Z")

</div>

default scaffolding shows some code reuse, if that helps.

---

<div class="post-metadata">

**Author:** ![predhme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e8c25b/32.png) [@predhme](https://www.ruby-forum.com/u/predhme)\
**Post date:** [April 30, 2008, 6:29pm UTC](https://www.ruby-forum.com/t/layouts-and-partials/136166/4 "2008-04-30T18:29:51Z")

</div>

I have constructed something to that effect…

layouts/global.html.erb

\<%= render :partial =\> “header” %\>  
\<%= render :partial =\> “navigation” %\>  
\<%= yield :actions %\>  
\<%= yield %\>  
\<%= render :partial =\> “footer” %\>

so for my views i need to declare the actions, most likely should be  
in a partial… the standard list  
\<% content\_for :actions %\>  
\<%= link\_to “New”, :action =\> “new” %\>  
\<%= link\_to “List”, :action =\> “list” %\>  
\<%= link\_to “Search”, :action =\> “search” %\>  
\<% end %\>

then i could have a set of other actions that aren’t ordinary. So i  
guess then in about 95% of my views i will have:  
\<%= render :partial =\> “default\_actions” %\>

and sometimes additional renders for other action sets I may need. Now  
my problem is that because I don’t always want to display the action  
menu, I cannot add the above render to my layout because then it would  
show all the time. So I would have to put the render in every one of  
my views when I want it. But seeing how it is needed more often than  
not, should I put the render call in my layout and a conditional  
statement to hide it when desired, I certainly do not want to clutter  
my views with conditional code when not needed. What would be the best  
way to handle this situation?

On Apr 29, 10:32 am, Thorsten M. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![predhme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e8c25b/32.png) [@predhme](https://www.ruby-forum.com/u/predhme)\
**Post date:** [April 30, 2008, 11:47pm UTC](https://www.ruby-forum.com/t/layouts-and-partials/136166/5 "2008-04-30T23:47:23Z")

</div>

Or multiple layouts. I’d tend to put it in the global layout, myself,  
so I don’t have to clutter the views with repeated code.
