1

In my Rails app I have an AdminController in app/controllers/admin:

module Admin
  class AdminController < ApplicationController

  end
end

And then inside the admin folder I have various other controllers which all inherit from the AdminController, e.g.

module Admin
  class PermissionsController < AdminController

  end
end

I'm trying to list all the controllers and actions under AdminController with:

def list
  @controllers = AdminController.descendants
end

But all I get is: Admin::PermissionsController

I've also tried AdminController.subclasses but get the same.

I don't want to do @controllers = Dir.new("#{Rails.root}/app/controllers/admin").entries because I also want to access the action_methods.

Why isn't descendants or subclasses working?

In my view I want to do:

<% @controllers.each do |controller| %>
    <h2><%= controller %></h2>
    <ul>
      <% controller.action_methods.each do |action| %>
          <li><%= action %></li>
      <% end %>
    </ul>
<% end %>

Which does list all the actions but only for the current controller and it also lists action methods from the parent controllers. I only want to list the ones for each controller.

Cameron
  • 27,963
  • 100
  • 281
  • 483
  • Possible duplicate of [Loading class descendants in rails development](http://stackoverflow.com/questions/29662518/loading-class-descendants-in-rails-development) – Slava.K Jan 18 '17 at 11:49
  • I think you'd get your answer right here http://stackoverflow.com/a/19173688/702436. The classes in rails are lazy loaded. Once you eager load them, `subclasses` should work for you. – Hass Jan 18 '17 at 11:52

0 Answers0