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.
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.
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?
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