0

My goal is to create quick urls for this one controller's views. Sort of like how some sites have domain/* mapping to viewing a user's profile. Beyond simply putting this rule in the routes rb at the end of the file, I would also like to prevent users from naming their URL as a controller inadvertently (or on purpose ha). So I need a quick way to grab all the names of the controllers and then I'll just write a custom validator in my model to tell the user that the specified url is reserved.

So how can I get a list of all the controllers?

Parris
  • 17,833
  • 17
  • 90
  • 133

3 Answers3

1

ActiveSupport adds a 'subclasses' property to the Class object, so you should be able to call ApplicationController.subclasses and get a list of all defined controllers. No guarantees this will be advisable.

Christopher Swasey
  • 10,392
  • 1
  • 31
  • 25
1

At the risk of sounding trivial, I'd say perhaps you can try to add the controller names in a array somewhere and do a check before creating the new user? That way the app won't waste resources listing out the directory everytime a user needs to be created.

Kashyap
  • 4,696
  • 24
  • 26
  • This is probably how it is going to need to happen when we scale up. For now I just read the directory, but once the code base stabalizes a bit more there would be nothing wrong with this approach. – Parris May 07 '12 at 19:12
0

I found another way to do it. Is this a bad idea? I feel like, its better than loading all the controllers needlessly?

controller_list = Array.new
Dir["app/controllers/*.rb"].each do |file|
    controller_list.push(file.split('/').last.sub!("_controller.rb",""))
end
Parris
  • 17,833
  • 17
  • 90
  • 133