# Making a separate admin zone

**URL:** <https://www.ruby-forum.com/t/making-a-separate-admin-zone/58492>\
**Category:** Rails\
**Created:** [April 22, 2006, 3:54am UTC](https://www.ruby-forum.com/t/making-a-separate-admin-zone/58492 "2006-04-22T03:54:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anabia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/35a633/32.png) [@anabia](https://www.ruby-forum.com/u/anabia)\
**Post date:** [April 22, 2006, 3:54am UTC](https://www.ruby-forum.com/t/making-a-separate-admin-zone/58492/1 "2006-04-22T03:54:21Z")

</div>

Hi, im building a little cms and i want to separate the admin zone  
controllers in a subfolder(controllers/admin) if possible to all be  
managed by one login module, i just have managed simple 3 or 4  
controller apps so any help you can give me will be very apreciated.

😉

---

<div class="post-metadata">

**Author:** ![anabia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/35a633/32.png) [@anabia](https://www.ruby-forum.com/u/anabia)\
**Post date:** [April 22, 2006, 4:15am UTC](https://www.ruby-forum.com/t/making-a-separate-admin-zone/58492/2 "2006-04-22T04:15:27Z")

</div>

Ana B. wrote:

> Hi, im building a little cms and i want to separate the admin zone  
> controllers in a subfolder(controllers/admin) if possible to all be  
> managed by one login module, i just have managed simple 3 or 4  
> controller apps so any help you can give me will be very apreciated.
> 
> 😉

I have a setup like this in the app I am working on now. The trick is  
to have one master admin controller with no action that the other admin  
controllers inherit form

First generate your controller with

ruby script/generate controller admin/admin

You should now have an Admin::AdminController in  
app/controllers/admin/admin.rb

My Admin:AdminController looks like this:

class Admin::AdminController \< ApplicationController  
layout ‘admin’  
before\_filter :admin\_required  
end

This way I am using a different layout and have a before filter that  
takes care of the authorization (admin\_required method is in  
application.rb since it used in other places on the site).

Now create a child controller that you will actually use

ruby script/generate controller admin/foo

And change the first line from:

class Admin::FooController \< ApplicationController

to

class Admin::FooController \< Admin::AdminController

and do the same for all controllers in your admin directory. This keeps  
your admin controllers clean and protected all from one spot.

---

<div class="post-metadata">

**Author:** ![anabia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/35a633/32.png) [@anabia](https://www.ruby-forum.com/u/anabia)\
**Post date:** [October 6, 2006, 3:21am UTC](https://www.ruby-forum.com/t/making-a-separate-admin-zone/58492/3 "2006-10-06T03:21:49Z")

</div>

I just followed the instructions above but am getting an error  
“undefined method `admin\_required’ for  
#Admin::ScheduleController:0x2783adc” when I go to /admin/page. What  
do I need to do for admin\_required? And how do I make it so I need to  
enter a login/password to access the admin pages?

---

<div class="post-metadata">

**Author:** ![anabia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/35a633/32.png) [@anabia](https://www.ruby-forum.com/u/anabia)\
**Post date:** [October 6, 2006, 4:27am UTC](https://www.ruby-forum.com/t/making-a-separate-admin-zone/58492/4 "2006-10-06T04:27:30Z")

</div>

John S. wrote:

> I just followed the instructions above but am getting an error  
> “undefined method `admin\_required’ for  
> #Admin::ScheduleController:0x2783adc” when I go to /admin/page. What  
> do I need to do for admin\_required? And how do I make it so I need to  
> enter a login/password to access the admin pages?

:admin\_required is a before\_filter, see the Rails API docs. It serves as  
a “gateway” method that’s run before any other methods in your  
controller can be accessed. You need to write that method yourself, and  
either put it in your Admin::Admin controller (so you can share it with  
your other controllers) or put it in your ApplicationController so you  
can share it with your entire app.

It could be named anything, by the way.

Example:

class Admin::AdminController \< ApplicationController  
layout ‘admin’  
before\_filter :admin\_required

```
def admin_required
    unless session[:user]
        #The user isn't logged in.
        redirect_to :controller=>'user', :action=>'login' and return 

```

false  
end  
end

end
