Avoiding repetitive "content_for" in views

actioncontroller, actionview, ruby-on-rails

Solution

My suggest is to have a convention for this, so if you have a `ProductsController` then the submenu would be `submenus/products_menu`. This way you can write a helper that looks like:

def render_submenu
  content_for(:submenu) { render :partial => "submenus/#{controller.controller_name}_menu" }
end

You can then call this by doing:

<%= render_submenu %>

You could then make this the default content_for the submenus and only specify the content if it needs to be different.

I hope this helps!

Problem

I have a submenu placed in my layout wich differs from controller to controller, but not between each controllers method views. What I am currently doing is the following: ``` <% content_for( :submenu ) do %> <%= render :partial => 'submenus/correct_submenu' %> <% end %> ``` In every view for a method My applications layout then has this in it ``` <%= yield :submenu %> ``` However, this feels kind of repetitive, doing it for each view. Is there some way to do this per controller?

Original source