0
votes

I am using rails_admin, devise, and cancan.

Users have many roles.

I have an admin page set up where a user with the admin role can manage users. However, admins cannot manage roles.

When creating or editing users, roles are not listed.

How do I allow admins to add roles to a user without letting them edit or create roles?

this is my ability file

class Ability
  include CanCan::Ability

  def initialize user
    if user && user.admin?
      can :access, :rails_admin
      can :dashboard
      cannot :manage, Role
      can :manage, User
    else
      cannot :access, :rails_admin
    end
  end
end

I tried explicitly overriding the association scope in the rails_admin config, but that didn't help at all:

  field :role do
    associated_collection_scope do
      Proc.new do |scope|
        scope = scope
      end
    end
  end
1

1 Answers

0
votes

I was able to make it work by adding can :index, Role but that isn't really desired as it makes Roles show up in the sidebar.

class Ability
  include CanCan::Ability

  def initialize user
    if user && user.admin?
      can :access, :rails_admin
      can :dashboard
      can :index,  Role
      can :manage, User
    else
      cannot :access, :rails_admin
    end
  end
end