Nice solution, but not very flexible. For such things I developed a plugin (site_map) which is hosted on rubyforge. You can install it with:
./script/plugin install svn://rubyforge.org/var/svn/site-map/trunk/site_map``
The basics are:
- in routes.rb define your site map:
SiteMap.draw do |map|
map.area 'public do
map.section 'about_us', :controller => 'frontend', :action => 'about_us'
map.section 'signup', :controller => 'account'
map.location :controller => 'frontend' # not bound to any particular section
end
map.area ‘admin’ do
map.section ‘users’, :controller => ‘/admin/users’
map.section ‘posts’ do
map.location :controller => ‘/admin/posts’
map.location :controller => ‘/admin/comments’
end
end
end
- Then in the views there is a current_location helper which returns an object that describes current location. It has all the location attributes (area name, section name, etc) initialized based on current request params (including controller and action names)
You can use any attributes you want (zone / area / section / etc), there is no predefined ones (except you can’t use “location” attribute, because it is reserved keyword).
In your views you can use some helpers like
<%= section_link ‘users’, ‘User management’, :controller => ‘/admin/users’ %>
which is defined in helpers as:
def section_link(section_name, title, url_options)
if current_location.section == section_name
# selected section
link_to title, url_options, :class => 'selected'
else
# normal section
link_to title, url_options
end
end
This saves me from adding new stylesheets if I add new subdivisions to my web applications. It also gives greater flexibility in mapping controllers AND actions to different “locations”. Also, you can render selected e.g. “section” with simple text (not a link). With css solution you can’t do that.