# Check if controller action?

**URL:** <https://www.ruby-forum.com/t/check-if-controller-action/160644>\
**Category:** Rails\
**Created:** [February 18, 2009, 1:38pm UTC](https://www.ruby-forum.com/t/check-if-controller-action/160644 "2009-02-18T13:38:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4af34b/32.png) [@rsm](https://www.ruby-forum.com/u/rsm)\
**Post date:** [February 18, 2009, 1:38pm UTC](https://www.ruby-forum.com/t/check-if-controller-action/160644/1 "2009-02-18T13:38:44Z")

</div>

Hi Chaps,

How can I check if the page is using a controllers action. for example i  
got a helper method that needs to know if a certain controller action is  
run on the page or not so as to display different content for it.

Thought it was just

code = “xyz” if Controllername.action?

Cheers,

Rog

---

<div class="post-metadata">

**Author:** ![rsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4af34b/32.png) [@rsm](https://www.ruby-forum.com/u/rsm)\
**Post date:** [February 18, 2009, 3:56pm UTC](https://www.ruby-forum.com/t/check-if-controller-action/160644/2 "2009-02-18T15:56:34Z")

</div>

On Feb 18, 1:38 pm, Roger M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Cheers,
> 
> ## Rog
> 
> Posted viahttp://www.ruby-forum.com/.

If I correctly understood what you need, you may try using:

> Controller.action\_methods.include?params[:action]  
> Where params[:action] will contain the slice of the url corresponding  
> to the action in the last request

---

<div class="post-metadata">

**Author:** ![rsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4af34b/32.png) [@rsm](https://www.ruby-forum.com/u/rsm)\
**Post date:** [February 18, 2009, 5:02pm UTC](https://www.ruby-forum.com/t/check-if-controller-action/160644/3 "2009-02-18T17:02:48Z")

</div>

Hi Roger,

On Wed, 2009-02-18 at 13:38 +0100, Roger M. wrote:

> Hi Chaps,
> 
> How can I check if the page is using a controllers action. for example i  
> got a helper method that needs to know if a certain controller action is  
> run on the page or not so as to display different content for it.
> 
> Thought it was just
> 
> code = “xyz” if Controllername.action?

Your views have access to the controller and method that rendered them  
via controller.controller\_name and controller.action\_name

e.g.,  
\<% if controller.controller\_name == ‘posts’ %\>  
\<% if controller.action\_name == ‘create’ %\>  
\<%= do whatever %\>  
\<% end %\>  
\<% end %\>

HTH,  
Bill
