# How to get current method/action name from layout rhtml

**URL:** <https://www.ruby-forum.com/t/how-to-get-current-method-action-name-from-layout-rhtml/72009>\
**Category:** Rails\
**Created:** [October 13, 2006, 4:57pm UTC](https://www.ruby-forum.com/t/how-to-get-current-method-action-name-from-layout-rhtml/72009 "2006-10-13T16:57:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![roseanne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@roseanne](https://www.ruby-forum.com/u/roseanne)\
**Post date:** [October 13, 2006, 4:57pm UTC](https://www.ruby-forum.com/t/how-to-get-current-method-action-name-from-layout-rhtml/72009/1 "2006-10-13T16:57:44Z")

</div>

I want to know what is current controller / action in my layout.rhtml  
shared by all my rhtml files to make some style changes.

I am new in ruby and RoR, I believe there must be a simple solution for  
this.

Any help would be great appreciated!!!

Roseanne

---

<div class="post-metadata">

**Author:** ![roseanne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@roseanne](https://www.ruby-forum.com/u/roseanne)\
**Post date:** [October 13, 2006, 5:19pm UTC](https://www.ruby-forum.com/t/how-to-get-current-method-action-name-from-layout-rhtml/72009/2 "2006-10-13T17:19:24Z")

</div>

There sure is a better soluition, but i added a before\_filter in my  
application controller like this:

before\_filter :set\_canames

…  
private  
def set\_canames  
@cname = controller\_name  
@aname = action\_name  
end

there probably is a better way, but it works.

---

<div class="post-metadata">

**Author:** ![roseanne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@roseanne](https://www.ruby-forum.com/u/roseanne)\
**Post date:** [October 13, 2006, 5:23pm UTC](https://www.ruby-forum.com/t/how-to-get-current-method-action-name-from-layout-rhtml/72009/3 "2006-10-13T17:23:25Z")

</div>

In your views, you can use controller.controller\_name and I’m sure there  
is  
a method for the action but I don’t know it off the top of my head.  
action\_name would make the most sense.

On 10/13/06, Thorsten L [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @cname = controller\_name  
> @aname = action\_name  
> end
> 
> there probably is a better way, but it works.
> 
> >

–  
-B

---

<div class="post-metadata">

**Author:** ![roseanne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@roseanne](https://www.ruby-forum.com/u/roseanne)\
**Post date:** [October 13, 2006, 5:46pm UTC](https://www.ruby-forum.com/t/how-to-get-current-method-action-name-from-layout-rhtml/72009/4 "2006-10-13T17:46:43Z")

</div>

Long wrote:

> Thorsten L wrote:

> controller.controller\_name  
> controller.action\_name
> 
> Long

Thanks everybody!!!  
Thanks Long!!!

It works great!

Roseanne

---

<div class="post-metadata">

**Author:** ![roseanne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@roseanne](https://www.ruby-forum.com/u/roseanne)\
**Post date:** [October 13, 2006, 5:29pm UTC](https://www.ruby-forum.com/t/how-to-get-current-method-action-name-from-layout-rhtml/72009/5 "2006-10-13T17:29:29Z")

</div>

Thorsten L wrote:

> @aname = action\_name  
> end
> 
> there probably is a better way, but it works.

There was a recent post with similar information. I believe you do have  
access to ‘controller’ in the views (and layouts) therefore it is  
possible to use

controller.controller\_name  
controller.action\_name

Long
