Automatically get a list of controller names


#1

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

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

domain.com/controllername

Ramon T.


#2

@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.


#3

@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.


#4

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.


#5

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.