Inheriting from AdminController intstead from ApplicationController

Hi

I would like to do the following:

I have created an admin namespace and the required folders app/admin views/admin etc. And then I wanted all controllers under app/admin to inherit from a controller named AdminController which resists under app/admin/admin.rb instead of inhereting from ApplicationController, so I could better separate between admin and public section. The AdminController looks like this:

module Admin   class AdminController < ActionController::Base     include AuthenticatedSystem     helper :all     protect_from_forgery     layout 'admin/layouts/admin'   end end

For testing the new namespace I created a GroupsController which looks like this:

require 'admin/admin' module Admin   class GroupsController < AdminController     def index       @groups = Group.find(:all)     end   end end

But every time I call the GroupsController I'm getting the following error:

uninitialized constant Admin::AdminController

Can anybody tell me what I'm doing wrong?

Thanks Mato

If AdminController is within admin/ I think your class name needs to include the namespace, Admin::AdminController. I've been putting together an 'admin' section myself by doing this and that's how I've been doing things.

Hope this helps, I'll keep my eye on this post to see how things go.

Dave K.

Hi

I agree with Dave. You Have to include Admin::AdminController. I think this should be the trick,else reply to this post again.

  • Rajesh

Hi

Thanks for your purposes but the problem still exists. The behaviour of the application has now changed. The first time I call a controller within the admin namespace it works fine, but when I reload the page the same error occurs again.

By the way, is it possible to configure rails to load the admin.rb file automaticly every time a controller in the admin namespace is called (like application.rb)?

Mato

Your controller rests under app/admin/admin.rb, which won’t be loaded automatically by rails because it doesn’t have a _controller suffix. I would suggest renaming this file to Admin::ApplicationController for its class and application_controller.rb as its file name, that way it will be automatically loaded and it will make sense what that controller’s supposed to be doing.

This solution works, but with one disadvantage. Now when I call http://mydomain.com/admin/application, the Admin::ApplicationController is called which shouldn't be possible. That's why I originally wanted to build something like the normal ApplicationController, so this one can't be called from outside.

If you’re really fussed with that, then define an index action in there that redirects people to where they should be. Not many people will be trying to go to admin/application unless they know it’s there.

I'm wondering whether you're trying to swim upstream with the inheritance approach inside a module. It seems you want to share some behaviors among your administrative controllers. Would a mixin work as well as direct inheritance here? E.g.:

module Admin    class AdminController < ApplicationController      include KewlAdminMethods    # ...    end end

Any chance that would solve your problem?

Hi

I think your right Ryan, not many people will try to go to admin/ applicaton.

s.ross: This would cause the same problems, because this file would also not be loaded properly.

Thank you guys for the help.

Mato

Admin is not a module, it’s a namespace, Admin::ApplicationController.

While the intent may be to create a namespace, it very much is a module. Here is the original code:

module Admin    class AdminController < ActionController::Base      include AuthenticatedSystem      helper :all      protect_from_forgery      layout 'admin/layouts/admin'    end end