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:
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.
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)?
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
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