1
votes

I'm setting up a basic rails app and integrating angularjs into it. Authentication is handled by devise, cancan and roles are managed by rolify. I'm trying to build a user index page and show the roles for each user on the page but can't seem to get it working. jbuilder returns the info for each user but the roles aren't in there.

index.json.jbuilder

json.array!(@users) do |user|
  json.id           user.id
  json.name         user.name
  json.email        user.user.email
  json.street       user.street
  json.city         user.city
  json.state        user.state
  json.zip          user.zip
  json.home_phone   user.home_phone
  json.cell_phone   user.cell_phone
  json.alt_email    user.alt_email
  json.start_year   user.start_year
  json.notes        user.notes
  json.confirmed    user.confirmed
  json.nickname     user.nickname
end

there's nothing special about my user model, controller or index page (I'm trying to show the roles in the index entry for the user on the index page)

any help?

thanks in advance:

Max

1

1 Answers

1
votes

I'm not familiar with jbuilder (I use Active Model Serializers to render json in my Angular-Rails Apps) but the reason you aren't seeing the roles is because Rolify adds roles to users not as an attribute on a User object but rather through a has_and_belongs_to_many association between the Role model and User model. So if you want to get a User's roles returned, you would need to create a Rails controller action that renders /users/:user_id/roles in json.

Then I would probably create a Role factory to initialize a $resource object in your Angular code and match it to that route so you can access roles for a user with a given id.