# Model-controller-paradigm: What about admin controllers?

**URL:** <https://www.ruby-forum.com/t/model-controller-paradigm-what-about-admin-controllers/78946>\
**Category:** Rails\
**Created:** [December 10, 2006, 10:33pm UTC](https://www.ruby-forum.com/t/model-controller-paradigm-what-about-admin-controllers/78946 "2006-12-10T22:33:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [December 10, 2006, 10:33pm UTC](https://www.ruby-forum.com/t/model-controller-paradigm-what-about-admin-controllers/78946/1 "2006-12-10T22:33:30Z")

</div>

Hi all

We all know the model-controller-paradigm: I have a model “News” which  
has a corresponding CRUD-controller “NewsController”.

But now I’m quite unsure about the following…

Guess we have normal visitors that visit our site www.??.com/news and we  
have administrators that create and modify news items.  
The admin should see an “Edit” link and a “Destroy” link corresponding  
to every news item, but visitors should not see them. So we mix some  
admin functions into the views.  
Further on we need some methods like www.??.com/news/edit/:id that  
should only be callable by an admin; we can easily secure this using a  
before\_filter or so.

So far so good. But the more complex our model becomes, the more we have  
to mix admin logic with visitor logic.  
So I guess it would be cleaner if we created not only one corresponding  
controller, but two of them: a NewsController that is an interface  
between data and visitors and a NewsAdminController that handles all the  
CRUD-and-more stuff an admin can do.  
Is this a good solution? I could then create a route that sends URL  
calls like www.??.com/news to the NewsController and URL calls like  
www.??.com/admin/news to the NewsAdminController…

Please tell me what you think about this. Are there better solutions for  
separating user logic and admin logic?

Thanks for your interesting answers,  
Joshua

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [December 11, 2006, 10:09pm UTC](https://www.ruby-forum.com/t/model-controller-paradigm-what-about-admin-controllers/78946/2 "2006-12-11T22:09:33Z")

</div>

alot of ppl are torn by this very issue. Personally, i find it easier  
to give the extra namespace by adding afolder called admins/ in the app  
directory and put all the admin functionality in there. It just looks  
nicer.

Im sure there are plenty of reasons this is wrong (REST and stuff like  
that), but i really dont care. It just ‘feels’ easier.

peace

–jake

Joshua M. wrote:

> Hi all
> 
> We all know the model-controller-paradigm: I have a model “News” which  
> has a corresponding CRUD-controller “NewsController”.
> 
> But now I’m quite unsure about the following…
> 
> Guess we have normal visitors that visit our site www.??.com/news and we  
> have administrators that create and modify news items.  
> The admin should see an “Edit” link and a “Destroy” link corresponding  
> to every news item, but visitors should not see them. So we mix some  
> admin functions into the views.  
> Further on we need some methods like www.??.com/news/edit/:id that  
> should only be callable by an admin; we can easily secure this using a  
> before\_filter or so.
> 
> So far so good. But the more complex our model becomes, the more we have  
> to mix admin logic with visitor logic.  
> So I guess it would be cleaner if we created not only one corresponding  
> controller, but two of them: a NewsController that is an interface  
> between data and visitors and a NewsAdminController that handles all the  
> CRUD-and-more stuff an admin can do.  
> Is this a good solution? I could then create a route that sends URL  
> calls like www.??.com/news to the NewsController and URL calls like  
> www.??.com/admin/news to the NewsAdminController…
> 
> Please tell me what you think about this. Are there better solutions for  
> separating user logic and admin logic?
> 
> Thanks for your interesting answers,  
> Joshua

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [December 11, 2006, 10:54pm UTC](https://www.ruby-forum.com/t/model-controller-paradigm-what-about-admin-controllers/78946/3 "2006-12-11T22:54:00Z")

</div>

jake wrote:

> alot of ppl are torn by this very issue. Personally, i find it easier  
> to give the extra namespace by adding afolder called admins/ in the app  
> directory and put all the admin functionality in there. It just looks  
> nicer.
> 
> Im sure there are plenty of reasons this is wrong (REST and stuff like  
> that), but i really dont care. It just ‘feels’ easier.

What exactly do you mean with REST? And are the controllers all found in  
this subfolder, or do you have to add it to the path?

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [December 16, 2006, 12:12pm UTC](https://www.ruby-forum.com/t/model-controller-paradigm-what-about-admin-controllers/78946/4 "2006-12-16T12:12:24Z")

</div>

> app/  
> –admin/  
> ----posts\_controller.rb \<= for administration  
> ----comments\_controller.rb  
> –post\_controller.rb \<= public facing  
> –comments controller

So they are called like this?

PostsAdminController  
CommentsAdminController  
PostsController  
AdminController

Right? Is there no problem for Rails to find the admin controllers? Do I  
have to adjust the path var or something?

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [December 14, 2006, 12:03am UTC](https://www.ruby-forum.com/t/model-controller-paradigm-what-about-admin-controllers/78946/5 "2006-12-14T00:03:36Z")

</div>

Joshua M. wrote:

> jake wrote:
> 
> > alot of ppl are torn by this very issue. Personally, i find it easier  
> > to give the extra namespace by adding afolder called admins/ in the app  
> > directory and put all the admin functionality in there. It just looks  
> > nicer.
> > 
> > Im sure there are plenty of reasons this is wrong (REST and stuff like  
> > that), but i really dont care. It just ‘feels’ easier.
> 
> What exactly do you mean with REST? And are the controllers all found in  
> this subfolder, or do you have to add it to the path?

REST (or Representational State Transfer) is a paradigm of software  
design. Check it out at:  
[Representational state transfer - Wikipedia](http://en.wikipedia.org/wiki/Representational_State_Transfer).

Only the editing part of the models is done in the admin controllers.  
Outside of the admin controllers are the public facing.

For example:

app/  
–admin/  
----posts\_controller.rb \<= for administration  
----comments\_controller.rb  
–post\_controller.rb \<= public facing  
–comments controller

–jake

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [December 16, 2006, 6:13pm UTC](https://www.ruby-forum.com/t/model-controller-paradigm-what-about-admin-controllers/78946/6 "2006-12-16T18:13:31Z")

</div>

Joshua M. wrote:

> > app/  
> > –admin/  
> > ----posts\_controller.rb \<= for administration  
> > ----comments\_controller.rb  
> > –post\_controller.rb \<= public facing  
> > –comments controller
> 
> So they are called like this?
> 
> PostsAdminController  
> CommentsAdminController  
> PostsController  
> AdminController
> 
> Right? Is there no problem for Rails to find the admin controllers? Do I  
> have to adjust the path var or something?

actually, they are called  
Admin::PostsController  
Admin::CommentsController  
PostsController  
CommentsController

so, when you do a link\_to to the admin section you would use:  
link\_to “Link Me”, :controller =\> “admin/posts”, :action =\> “edit\_post”,  
:id =\> 1

Most of the time, you will never do the above because you usually dont  
want to link from the public facing side to the admin side.

–jake
