Say I have 4 roles: user, agent, admin, superadmin.

Where each role has subsequently more permissions on all objects in my app.

I am using Rolify, CanCan & Devise.

What I would like to do is, whenever I want to enable something - say:

<%= link_to "Create New User", new_user_registration_path if current_user.has_role? :superadmin %>

I would like for that link to show if the person is either an admin or a superadmin. I know I could simply just do an || - but that doesn't seem very DRY, especially since I will have to do current_user.has_role? :superadmin || current_user.has_role? :admin.

Imagine there are some links/assets that I want agent, admin & superadmin to have access to.

How do I do this in a DRY way?

有帮助吗?

解决方案

Why do u check roles instead of checking a permissions? As I know cancan support can? and cannot? methods which can help in your situation. You should write something like this

<%= link_to "Create New User", new_user_registration_path if can?(:create, User) %>

Usefull link about this feature

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top