I know this is an old thread, but I just ran into the need to have separate navigation depending on the namespace of the controller. The solution I came up with was in my application layout:
<%= render "#{controller.class.name[/^(\w*)::\w*$/, 1].try(:downcase)}/nav" %>
Which looks a bit more complicated, but basically does the following: for the controller name, the class name is used, which will be, for example, "People" for the controller without names, and "Admin :: Users" for the namespace. Using the [] string method with a regex that returns something before two colons, or nil if not. Then he changes it to lowercase ("try" is where there is no namespace and nil is returned). This leaves us with either a namespace or nil. Then it simply displays partial with or without a namespace, for example without a namespace:
app/views/_nav.html.erb
or in the admin namespace:
app/views/admin/_nav.html.erb
Of course, these partial must exist for each namespace, otherwise an error occurs. Now for each namespace navigation will be displayed for each controller without the need to change any controller or view.
Dave Hollingworth May 6 '10 at 8:12 a.m. 2010-05-06 08:12
source share