Question

I'm using declarative roles, a Ryan Bates 'railscast' tutorial. I would like to show the role of the user who is logged in, in the view. Is there any variable I can use to display the name of the role?

Was it helpful?

Solution

As that tutorial goes, a user "has many" roles, but you are not logged in as a particular role at any one time, you have all of them assigned to you, and all of those assigned are valid all at once. They are linked from the user model:

models/user.rb

has_many :assignments
has_many :roles, :through => :assignments

So you could show all the roles with something like

<%= current_user.roles.join(', ') %>

Assuming that current_user is a helper method that gives the current logged in user model instance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top