# Automatically get a list of controller names

**URL:** <https://www.ruby-forum.com/t/automatically-get-a-list-of-controller-names/161514>\
**Category:** Rails\
**Created:** [February 27, 2009, 2:50am UTC](https://www.ruby-forum.com/t/automatically-get-a-list-of-controller-names/161514 "2009-02-27T02:50:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramontayag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@ramontayag](https://www.ruby-forum.com/u/ramontayag)\
**Post date:** [February 27, 2009, 2:50am UTC](https://www.ruby-forum.com/t/automatically-get-a-list-of-controller-names/161514/1 "2009-02-27T02:50:33Z")

</div>

Okay, just a follow up question: is there an automatic way to make an  
array of all controller names? So that I could use this in the user  
model to validate the user doesn’t type in any of the controller  
names.

(This is a continued conversation of one that asked about making  
routes this way:

[domain.com/username](http://domain.com/username)

but still retaining the functionality to send the user to a controller  
if they typed:

[domain.com/controllername](http://domain.com/controllername)

Ramon T.

---

<div class="post-metadata">

**Author:** ![ramontayag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@ramontayag](https://www.ruby-forum.com/u/ramontayag)\
**Post date:** [February 27, 2009, 4:22am UTC](https://www.ruby-forum.com/t/automatically-get-a-list-of-controller-names/161514/2 "2009-02-27T04:22:08Z")

</div>

@conts\_and\_meths = []  
controllers = Dir.new("#{RAILS\_ROOT}/app/controllers").entries  
controllers.each do |controller|  
if controller =~ /\_controller/ and not controller.index(".swp")  
then  
cont = controller.camelize.gsub(".rb","")  
meths = (eval("#{cont}.new.methods") -  
ApplicationController.methods - Object.methods -  
ApplicationController.new.methods).sort  
@conts\_and\_meths \<\< [cont, meths]  
end

This gives you the methods too, but you can see how you would just get  
the controllers if you wanted. Careful, this is not stuff you want to  
expose to everybody.

---

<div class="post-metadata">

**Author:** ![ramontayag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@ramontayag](https://www.ruby-forum.com/u/ramontayag)\
**Post date:** [February 27, 2009, 4:22am UTC](https://www.ruby-forum.com/t/automatically-get-a-list-of-controller-names/161514/3 "2009-02-27T04:22:43Z")

</div>

@conts\_and\_meths = []  
controllers = Dir.new("#{RAILS\_ROOT}/app/controllers").entries  
controllers.each do |controller|  
if controller =~ /\_controller/ and not controller.index(".swp")  
then  
cont = controller.camelize.gsub(".rb","")  
meths = (eval("#{cont}.new.methods") -  
ApplicationController.methods - Object.methods -  
ApplicationController.new.methods).sort  
@conts\_and\_meths \<\< [cont, meths]  
end

This gives you the methods too, but you can see how you would just get  
the controllers if you wanted. Careful, this is not stuff you want to  
expose to everybody.

---

<div class="post-metadata">

**Author:** ![ramontayag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@ramontayag](https://www.ruby-forum.com/u/ramontayag)\
**Post date:** [February 27, 2009, 8:20pm UTC](https://www.ruby-forum.com/t/automatically-get-a-list-of-controller-names/161514/4 "2009-02-27T20:20:41Z")

</div>

I would recommend that you hard-code that list and also include  
controller names you may use in the future and reserved or restricted  
words.

---

<div class="post-metadata">

**Author:** ![ramontayag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@ramontayag](https://www.ruby-forum.com/u/ramontayag)\
**Post date:** [August 21, 2009, 8:51pm UTC](https://www.ruby-forum.com/t/automatically-get-a-list-of-controller-names/161514/5 "2009-08-21T20:51:11Z")

</div>

I have this bad habit of “starring” (gmail) messages and forgetting to  
get back to it. Better late than never. Thanks jasoo and willhaslett!

Ramon T.
