# Accessing name of controller and action from layout

**URL:** <https://www.ruby-forum.com/t/accessing-name-of-controller-and-action-from-layout/68324>\
**Category:** Rails\
**Created:** [September 11, 2006, 6:46pm UTC](https://www.ruby-forum.com/t/accessing-name-of-controller-and-action-from-layout/68324 "2006-09-11T18:46:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mountaindrummer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bb73d2/32.png) [@mountaindrummer](https://www.ruby-forum.com/u/mountaindrummer)\
**Post date:** [September 11, 2006, 6:46pm UTC](https://www.ruby-forum.com/t/accessing-name-of-controller-and-action-from-layout/68324/1 "2006-09-11T18:46:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mountaindrummer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bb73d2/32.png) [@mountaindrummer](https://www.ruby-forum.com/u/mountaindrummer)\
**Post date:** [September 11, 2006, 6:53pm UTC](https://www.ruby-forum.com/t/accessing-name-of-controller-and-action-from-layout/68324/2 "2006-09-11T18:53:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mountaindrummer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bb73d2/32.png) [@mountaindrummer](https://www.ruby-forum.com/u/mountaindrummer)\
**Post date:** [September 11, 2006, 7:56pm UTC](https://www.ruby-forum.com/t/accessing-name-of-controller-and-action-from-layout/68324/3 "2006-09-11T19:56:50Z")

</div>

\<%= self.controller.action\_name.eql?(‘my\_page’) ? ‘current’ :  
self.controller.action\_name %\>

---

<div class="post-metadata">

**Author:** ![mountaindrummer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bb73d2/32.png) [@mountaindrummer](https://www.ruby-forum.com/u/mountaindrummer)\
**Post date:** [September 11, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/accessing-name-of-controller-and-action-from-layout/68324/4 "2006-09-11T22:01:43Z")

</div>

You can also check the params like so:

class="\<%= (params[:controller] == ‘my\_page’ ? ‘current’ :  
‘not-current’) %\>"
