I have a dropdown for changing the role of a user in my Rails 4.2 app with CanCanCan. The User::ROLES array of strings holds the different possible roles that a user can have. To display the roles in a dropdown:
<%= f.select :role, options_for_select((User::ROLES), @user.role) %>
I want to limit the dropdown options to roles that the currently signed-in user can :manage
according to CanCanCan. Roles that the current user does not have :manage
permissions on should not be populated in the dropdown. Any guidance is appreciated.
edit:
Sean Huber's answer is perfect if comparing against the string literals. However, I realized the issue is that I define this array in my User model, but I need to be comparing against the roles that are set one level deeper in ability.rb
. Here's an example of how permissions on the User model are set in ability.rb
:
This makes sense, however I realized it won't really work since it's comparing to the literals in the User::ROLES array.
The code in ability.rb
that sets roles access looks like this. I need to check against the specific role specified:
if user.role? :superadmin
can :manage, User, role: 'admin'
end
How can I compare against the specific roles that the :superadmin
tokenized role has :manage
access over instead of the literals in User::ROLES
?
The definition of ROLES from User.rb:
ROLES = %w[student teacher school district reseller admin superadmin god]