Blog » Blog Entry

Rubyonrails model and controller lists:

Updated: June 12, 2007February 6, 2007

In your Rubyonrails app you may at some point need a list of all your controllers, here's how I do it:

def get_controllers
  controllers = []
  glob = RAILS_ROOT + '/app/controllers/*_controller.rb'
  Dir.glob(glob).each do |f|
    file = File.basename(f).gsub( /^(.+)_controller.rb/, '\1')
    controllers << file
  end
  controllers
end

Getting a list of all your models is similar:

def get_models
  models = []
  glob = RAILS_ROOT + '/app/models/*'
  Dir.glob(glob).each do |f|
    file = File.basename( f ).gsub( /^(.+).rb/, '\1')
    models << file
  end
  models
end
Tags: ruby, rubyonrails, model, controller

« Rubyonrails get public actions from a controller Migration to Lighttpd »

thanks

By: ROR <jomillergo at gmail dot com>

Website: http://about.infocrystals.com/rubyonrails_installation.asp

Posted: 9 months ago

will this list all the controllers lemme try.

Add a comment:

Title:

Comment:

Name:

Email:

Website: