문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top