Patterns for implementing "admin" functionality to rails' models and controller "layouts"

Hello list,

In my application, I’ve followed the following pattern - for each model, I have a correspondent controller. For example, I’ve got a Document mode, and a DocumentsController, a DocType model and DocTypesController and so on… Should I just put new admin actions into these controllers ?

I’m not sure if the pattern I’ve followed for the controllers is the ideal one:

  • Maybe I should put these controllers into a admin subdir/module and create other controllers for other data access levels (not admin).

  • Maybe a model does not have to necessarily have a correspondent controller;

  • Maybe this way of thinking is limiting!

So, any suggestions on how could I lay out my controllers and also the admin aspect of the application would be greatly appreciated!

Thanks,

Marcelo.

I don't recommend using nested controllers (controllers in subdirs below app/controllers). If you want nested url's you can achieve that with routes.

There does not have to be a 1-1 correspondence between models and controllers.

You can put your admin actions in the same controller as the normal actions; it makes your before_filter specification a bit more complex. The nice thing about having a separate controller for admin actions is that you can use a catch-all before_filter to require admin login. You can even create an "abstract" admin controller with the before_filter and then have all your admin-specific controllers inherit from that one:

   class AdminController < ApplicationController       before_filter :require_admin_login    end

   class WidgetAdminController < AdminController       ...    end

Then you could route (for example):

   /widget/:action/:id to WidgetController    /admin/widget/:action/:id to WidgetAdminController

You get the idea...

I too have been creating nested admin controllers and thinking there had to be a better way.

   class AdminController < ApplicationController       before_filter :require_admin_login    end

   class WidgetAdminController < AdminController       ...    end

This is an excellent idea.

Then you could route (for example):

   /widget/:action/:id to WidgetController    /admin/widget/:action/:id to WidgetAdminController

Thanks!

Thanks for the tips Bob!

So, if I understood, for each controller with “regular” actions that I would want admin functionality, I would create a correspondent admin controller?

i.e DocumentController

DocumentAdminController

Thanks again,

Marcelo.