Layouts and partials

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?

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.

http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#M001069

default scaffolding shows some code reuse, if that helps.

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-

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs