Imagine to have two RESTful controllers (UsersController, OffersController) and a PagesController (used for static content such as index, about and so on) in your application.
You have the following routes defined:
map.with_options :controller => 'pages' do |pages|
pages.root :action => 'index' # static home page
pages.about :action => 'about' # static about page
# maybe more static pages...
end
map.resources :users # RESTful UsersController
map.resources :posts # RESTful PostsController
Your application layout looks like this:
<html>
<head>
<title>Demo Application</title>
</head>
<body>
<ul id="menu">
<li>
<%= link_to 'Home', root_path %>
</li>
<li>
<%= link_to 'Offers', offers_path %>
<ul id="submenu>
<li><%= link_to 'Search', 'path/to/search' %></li>
<li>maybe more links...</li>
</ul>
</li>
<li>
<%= link_to 'About', about_path %>
</li>
<li>
<%= link_to 'Admin', users_path %>
<ul id="submenu">
<li><%= link_to 'New User', new_user_path %></li>
<li><%= link_to 'New Offer', new_offer_path %></li>
<li>maybe more links</li>
</ul>
</li>
</li>
<%= yield %>
</body>
</html>
The problem with the layout is that I want only one #submenu
to be visible at any time. All other submenus can be completely skipped (don't need to rendered at all).
Take the Admin menu for example: This menu should be active for all RESTful paths in the application except for offers_path
. Active means that the submenu is visible.
The only solution I can think of to achieve this is to build a lot complicated if conditions and that sucks (really complicated to write and to maintain). I'm looking for an elegant solution?
I hope someone understands my question - if there's something unclear just comment the question and I'm going to explain it in more detail.