I have a User that can have multiple roles player
, coach
, guest
.
I want to have different views of a player_profile (profile/8)
depending on the user that is logged in or viewing that profile.
Aside from writing a bunch of if
statements in my views to detect cancancan
permissions, is there a simpler/more sane way to tackle this in as DRY as fashion as possible?
To display different views based on user role, you can employ some simple branching logic in the controller. Let's take the show
action as an example:
def show
if current_user.role == 'admin'
render 'show_admin'
else
render 'show'
end
end
This example assumes that you have access to a current_user
helper, either from something like Devise or your own solution, a show_admin.html.erb file
, a show.html.erb
file and a role
attribute on your User
model.
There isn't really that much to it.