CanCan的管理员授权

Kyl*_*cot 8 ruby-on-rails cancan ruby-on-rails-3

A有一堆带有Admin命名空间的控制器.我想限制访问这些,除非用户是管理员.有没有办法使用CanCan做到这一点,而不必打电话给未经授权!在每个控制器的每个方法?

mar*_*ark 8

将应用程序控制器添加到您的命名空间,并在之前过滤它.

class ApplicationController < ActionController::Base
end

class Admin::ApplicationController < ApplicationController 
  # these goes in your namespace admin folder
  before_filter :check_authorized

  def check_authorized
    redirect_to root_path unless can? :admin, :all
  end
end

class SomeadminController < Admin::ApplicationController
   def some_action
     # do_stuff
   end
end
Run Code Online (Sandbox Code Playgroud)