воскресенье, сентября 10, 2006

Site map

How do you divide site into areas ? How do you highlight current link ?

Definition

You've got site with several navigation bars, e.g. top menu and sidebar. You want current location highlighted on both top menu and sidebar.

Analysis

Rails has link_to_unless_current method to deal with such things, but it will not be enough: it handles link to only one page, but you need the bunch of pages be linked to. Link to can become selected only on one of the page, but on others it will not be selected (because they have different urls).

Solution

My solution was to develop some site structure markup that could associate each page with some "area" or "section" or kind of..

I wrote site_map plugin that handles that:

SiteMap.draw do |map|
map.area 'member' do
map.section 'profile', :controller => 'member' do
['profile', 'profile_update',
'company_profile', 'company_profile_update'
].each do |action|
map.location :action => action
end
end
end

map.area 'admin' do
map.section 'users', :controller => 'users'
map.section 'products', :controller => 'products'
end
end

In controller and in view there is a current_location method available that returns a location descriptor - object, that has area, section, etc. values populated with names that correspond to current page. You can use it to find out what link should be "selected":

<%= link_to_if current_location.section == 'users',
'User management', :controller => 'users' %>

2 комментария:

Анонимный комментирует...

Perfect Maxim! Just what I need -- now where can I find your plugin?

Unknown комментирует...

Thanks for showing interest in my plugin. You can find this plugin at RubyForge (site-map project).

I was lazy enough to add project's home page before. But now it is there with simple examples and installation instructions.

Hope you'll find it useful.