Forum: Ruby on Rails Automatically get a list of controller names

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fbd9cb107fe7c941333d6a3488691989?d=identicon&s=25 Ramon Tayag (ramontayag)
on 2009-02-27 02:50
(Received via mailing list)
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 Tayag
B11d21caa403bf6d46b0b29afa64ae70?d=identicon&s=25 willhaslett (Guest)
on 2009-02-27 04:22
(Received via mailing list)
@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.
B11d21caa403bf6d46b0b29afa64ae70?d=identicon&s=25 willhaslett (Guest)
on 2009-02-27 04:22
(Received via mailing list)
@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.
F999a7c806afd4b1d53f9fed9a18c704?d=identicon&s=25 jasoo24 (Guest)
on 2009-02-27 20:20
(Received via mailing list)
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.
Fbd9cb107fe7c941333d6a3488691989?d=identicon&s=25 Ramon Tayag (ramontayag)
on 2009-08-21 20:51
(Received via mailing list)
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 Tayag
This topic is locked and can not be replied to.