Forum: Ruby on Rails Making a separate admin zone

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ana B. (Guest)
on 2006-04-22 05:54
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.

;)
Alex W. (Guest)
on 2006-04-22 06:15
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.
John S. (Guest)
on 2006-10-06 05:21
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?
Daniel C. (Guest)
on 2006-10-06 06:27
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
This topic is locked and can not be replied to.