如何在CanCan中设置管理员角色?

时间:2013-05-05 15:19:55

标签: ruby-on-rails ruby-on-rails-3 cancan

我想为此添加管理员角色。管理员应该能够销毁并更新所有内容 管理员为user.id = 1

我如何在此编码?

模型/ ability.rb

if user
    .....
    can :read, :all 
    can [:create, :destroy], Comment, {:user_id => user.id}
    can [:destroy], Comment, {:commentable_id => user.id, :commentable_type => user.class.name}
    can [:create, :update], Community, {:user_id => user.id}
    .....
else
    can :read, :all 
end

1 个答案:

答案 0 :(得分:0)

你是如此接近它甚至都不好笑。

您正在寻找can :manage, :all

if user
  if user.admin? # or user.id == 1
    can :manage, :all
  else
    .....
    can :read, :all 
    can [:create, :destroy], Comment, {:user_id => user.id}
    can [:destroy], Comment, {:commentable_id => user.id, :commentable_type => user.class.name}
    can [:create, :update], Community, {:user_id => user.id}
    .....
  else
    can :read, :all 
end