Active_admin works with cancan

阅读更多
这半年多一直在做rails相关的东西,后台也一直用的 active_admin,感觉还是很方便的,而且可定制性比rails_admin高,不过最近需要让active_admin和cancan好好地一起工作,找了些资料 ,终于实现了,发现还是比较简单的,我已经加到active_admin的wiki了

以网站权限设置编辑人员只能管理新闻,而且只能加和看,不能删
1.app/models/ability.rb
class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= AdminUser.new       
    case user.role      
      when "admin"
        can :manage, :all
      when "editor"
        can :manage,Post   
        cannot [:destroy,:edit], Post   
      end
  end 
end  


2.app/helpers/application_controller.rb
 def current_ability
    @current_ability ||= Ability.new(current_admin_user)
  end


3.app/admin/admin_user.rb
# encoding: UTF-8
ActiveAdmin.register AdminUser do    
  menu :if => proc{can? :manage,AdminUser}     
  controller.authorize_resource 
end 


如果要对其它model设置权限 ,和admin_user.rb一样设置就可以了

转载注明:

iteye--- doabit

你可能感兴趣的:(cancan,rails,active_admin)