Ruby on rails 3 如何使用CanCan实现动态授权?

Ruby on rails 3 如何使用CanCan实现动态授权?,ruby-on-rails-3,devise,cancan,Ruby On Rails 3,Devise,Cancan,我遵循这个教程。我已经通过Desive创建了用户,我希望当用户以管理员身份登录时,他应该拥有用户列表,可以在其中编辑用户的权限,如edit destroy等 提前感谢您可以在设计模型调用角色中添加一个字段。然后在您的模型中创建一个能力类,然后定义您的授权,如 class Ability include CanCan::Ability def initialize(user) # Define abilities for the passed in user here. For ex

我遵循这个教程。我已经通过Desive创建了用户,我希望当用户以管理员身份登录时,他应该拥有用户列表,可以在其中编辑用户的权限,如edit destroy等


提前感谢

您可以在设计模型调用角色中添加一个字段。然后在您的模型中创建一个能力类,然后定义您的授权,如

class Ability
  include CanCan::Ability

  def initialize(user)
  # Define abilities for the passed in user here. For example:
     user ||= Login.new # guest user (not logged in)
     if user.role == 'admin'
       can :manage, :all
     else
       can :read, :all
     end
  end
end 
有关详细信息,请参见wiki: