# How to find out which actions are available on a controller?

**URL:** <https://www.ruby-forum.com/t/how-to-find-out-which-actions-are-available-on-a-controller/48373>\
**Category:** Rails\
**Created:** [November 19, 2005, 3:06pm UTC](https://www.ruby-forum.com/t/how-to-find-out-which-actions-are-available-on-a-controller/48373 "2005-11-19T15:06:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jochen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/91b2a8/32.png) [@jochen](https://www.ruby-forum.com/u/jochen)\
**Post date:** [November 19, 2005, 3:06pm UTC](https://www.ruby-forum.com/t/how-to-find-out-which-actions-are-available-on-a-controller/48373/1 "2005-11-19T15:06:37Z")

</div>

Hi!

How can I find out which actions a controller supports? I want something  
like

is\_available( :controller =\> ‘foo’, :action =\> ‘bar’ )

which tells me whether a link to

url\_for( :controller =\> ‘foo’, :action =\> ‘bar’ )

will succeed.

I want to dynamically place links on a page to actions on some specific  
controller, but only when this action is actually supported.

## Jochen

Jochen Topf removed\_email\_address@domain.invalid [http://www.remote.org/jochen/](http://www.remote.org/jochen/)  
+49-721-388298

---

<div class="post-metadata">

**Author:** ![jochen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/91b2a8/32.png) [@jochen](https://www.ruby-forum.com/u/jochen)\
**Post date:** [November 19, 2005, 4:16pm UTC](https://www.ruby-forum.com/t/how-to-find-out-which-actions-are-available-on-a-controller/48373/2 "2005-11-19T16:16:15Z")

</div>

Jochen Topf wrote:

> will succeed.
> 
> I want to dynamically place links on a page to actions on some specific  
> controller, but only when this action is actually supported.

Since actions are just methods you could use ruby reflection to get all  
methods of the controller:

#!/usr/bin/ruby

class Test  
def ciao  
print “ciao!\n”  
end  
end

class Hello  
#all methods that the class supports:  
#print a.methods.sort.join("\n")

#public instance methods:  
print Test.public\_instance\_methods(false)  
print “\n”

#or use the respond\_to? method to see if a known  
#method is supported  
#print a.respond\_to?(:ciao)  
end

---

<div class="post-metadata">

**Author:** ![jochen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/91b2a8/32.png) [@jochen](https://www.ruby-forum.com/u/jochen)\
**Post date:** [November 19, 2005, 4:28pm UTC](https://www.ruby-forum.com/t/how-to-find-out-which-actions-are-available-on-a-controller/48373/3 "2005-11-19T16:28:20Z")

</div>

Jochen Topf wrote:

> will succeed.
> 
> I want to dynamically place links on a page to actions on some specific  
> controller, but only when this action is actually supported.
> 
> Jochen

If you’re inside the controller you can call  
action\_methods.include?(‘bar’). From the outside, it’s  
FooController.action\_methods.include?(‘bar’).

– stefan

---

<div class="post-metadata">

**Author:** ![jochen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/91b2a8/32.png) [@jochen](https://www.ruby-forum.com/u/jochen)\
**Post date:** [November 19, 2005, 7:38pm UTC](https://www.ruby-forum.com/t/how-to-find-out-which-actions-are-available-on-a-controller/48373/4 "2005-11-19T19:38:27Z")

</div>

On 19-nov-2005, at 19:14, Jochen Topf wrote:

> > > Jochen  
> > > things look a bit nicer and consistent with other functions. I am sure  
> > > there must be a function for this somewhere in Rails, because it  
> > > has to  
> > > do it itself for each request, but I can’t find it.

Jochen, you can take a look at the Inflector - methods as  
Inflector::modulize, symbolize, demodulize, constantize etc.  
However, it “feels” to me that you are trying to do something which  
already IS in rails, you just not know how to get to it.

For instance, have you thought about asking Routes if your route is  
mappable and hiding the link if it’s not there? I think if you could  
do that  
you would get away with much less code that what you are trying to do  
now.

---

<div class="post-metadata">

**Author:** ![jochen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/91b2a8/32.png) [@jochen](https://www.ruby-forum.com/u/jochen)\
**Post date:** [November 19, 2005, 7:17pm UTC](https://www.ruby-forum.com/t/how-to-find-out-which-actions-are-available-on-a-controller/48373/5 "2005-11-19T19:17:27Z")

</div>

On Sat, Nov 19, 2005 at 04:27:39PM +0100, Stefan K. wrote:

> > 
> 
> If you’re inside the controller you can call  
> action\_methods.include?(‘bar’). From the outside, it’s  
> FooController.action\_methods.include?(‘bar’).

Thanks. That solves half of the problem. But is there also a way to get  
from the string ‘foo’ to the controller ‘FooController’? This would make  
things look a bit nicer and consistent with other functions. I am sure  
there must be a function for this somewhere in Rails, because it has to  
do it itself for each request, but I can’t find it.

## Jochen

Jochen Topf removed\_email\_address@domain.invalid [http://www.remote.org/jochen/](http://www.remote.org/jochen/)  
+49-721-388298

---

<div class="post-metadata">

**Author:** ![jochen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/91b2a8/32.png) [@jochen](https://www.ruby-forum.com/u/jochen)\
**Post date:** [November 19, 2005, 8:41pm UTC](https://www.ruby-forum.com/t/how-to-find-out-which-actions-are-available-on-a-controller/48373/6 "2005-11-19T20:41:22Z")

</div>

On Sat, Nov 19, 2005 at 07:35:10PM +0100, Julian ‘Julik’ Tarkhanov  
wrote:

> > > > controller, but only when this action is actually supported.  
> > > > from the string ‘foo’ to the controller ‘FooController’? This would
> 
> For instance, have you thought about asking Routes if your route is  
> mappable and hiding the link if it’s not there? I think if you could  
> do that  
> you would get away with much less code that what you are trying to do  
> now.

Well, how would I ask Routes if my route is mappable? I couldn’t find  
any documentation about this.

## Jochen

Jochen Topf removed\_email\_address@domain.invalid [http://www.remote.org/jochen/](http://www.remote.org/jochen/)  
+49-721-388298

---

<div class="post-metadata">

**Author:** ![jochen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/91b2a8/32.png) [@jochen](https://www.ruby-forum.com/u/jochen)\
**Post date:** [November 20, 2005, 4:56am UTC](https://www.ruby-forum.com/t/how-to-find-out-which-actions-are-available-on-a-controller/48373/7 "2005-11-20T04:56:53Z")

</div>

Jochen Topf wrote:

> > > like  
> > > controller, but only when this action is actually supported.
> 
> Thanks. That solves half of the problem. But is there also a way to get  
> from the string ‘foo’ to the controller ‘FooController’? This would make  
> things look a bit nicer and consistent with other functions. I am sure  
> there must be a function for this somewhere in Rails, because it has to  
> do it itself for each request, but I can’t find it.
> 
> Jochen

Fo top level controllers you can do

```
Module.const_get('foo'.camelize + "Controller")

```

If your controller lives inside a module, with a patch like “admin/foo”,  
it’s probably

```
Module.const_get("admin/foo".split("/").collect!{|m|

```

m.camelize}.join("::") + “Controller”)

But I’m not sure, as I haven’t used modules yet. Maybe there’s a builtin  
function for this.

– stefan
