Hi,
I am generating a navigation bar on top of my page in the application
layout. I want to set the class name for a navigation item to “current”
if the current page displayed is associated with that navigation item so
it can be made to look like it is selected by the stylesheet settings.
What is the best way for me to access the name of the controller and the
action so I could implement this logic in the layout?
I tried the method controller_class_name, but it was an undefined
variable.
Is there a better way to structure this than what I am trying to do?
Thanks,
Ryan
Mountain Drummer wrote:
Hi,
I am generating a navigation bar on top of my page in the application
layout. I want to set the class name for a navigation item to “current”
if the current page displayed is associated with that navigation item so
it can be made to look like it is selected by the stylesheet settings.
What is the best way for me to access the name of the controller and the
action so I could implement this logic in the layout?
I tried the method controller_class_name, but it was an undefined
variable.
Is there a better way to structure this than what I am trying to do?
Thanks,
Ryan
Hi Ryan,
You can have some before_filter :set_names call in your application
controller. Then those variables will be accessible from your views.
def set_names
@controller_name = controller_name()
@action_name = action_name()
end
Just for info, there is a link_to_unless_current helper.
Jean-Etienne
<%= self.controller.action_name.eql?(‘my_page’) ? ‘current’ :
self.controller.action_name %>
You can also check the params like so:
class="<%= (params[:controller] == ‘my_page’ ? ‘current’ :
‘not-current’) %>"