# Patterns for implementing "admin" functionality to rails' mo

**URL:** <https://www.ruby-forum.com/t/patterns-for-implementing-admin-functionality-to-rails-mo/113405>\
**Category:** Rails\
**Created:** [August 30, 2007, 7:51pm UTC](https://www.ruby-forum.com/t/patterns-for-implementing-admin-functionality-to-rails-mo/113405 "2007-08-30T19:51:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mhfs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@mhfs](https://www.ruby-forum.com/u/mhfs)\
**Post date:** [August 30, 2007, 7:51pm UTC](https://www.ruby-forum.com/t/patterns-for-implementing-admin-functionality-to-rails-mo/113405/1 "2007-08-30T19:51:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mhfs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@mhfs](https://www.ruby-forum.com/u/mhfs)\
**Post date:** [August 30, 2007, 8:54pm UTC](https://www.ruby-forum.com/t/patterns-for-implementing-admin-functionality-to-rails-mo/113405/2 "2007-08-30T20:54:42Z")

</div>

On 8/30/07, Marcelo de Moraes S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> - 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!

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…

---

<div class="post-metadata">

**Author:** ![mhfs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@mhfs](https://www.ruby-forum.com/u/mhfs)\
**Post date:** [August 30, 2007, 9:26pm UTC](https://www.ruby-forum.com/t/patterns-for-implementing-admin-functionality-to-rails-mo/113405/3 "2007-08-30T21:26:44Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![mhfs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@mhfs](https://www.ruby-forum.com/u/mhfs)\
**Post date:** [August 30, 2007, 10:04pm UTC](https://www.ruby-forum.com/t/patterns-for-implementing-admin-functionality-to-rails-mo/113405/4 "2007-08-30T22:04:05Z")

</div>

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.
