user1886476
user1886476

Reputation: 25

Rails 3, HABTM, and Displaying data

I am working with three tables; users, roles, and roles_users in a HABTM setup.

I am trying to display all users with their roles.

user.rb Model

class User < ActiveRecord::Base

  has_and_belongs_to_many :roles
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, # :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email,:username, :password, :password_confirmation, :remember_me,     :role_ids
  # attr_accessible :title, :body

  def role?(role)
    return !!self.roles.find_by_name(role.to_s.camelize)
  end

end

role.rb Model

 class Role < ActiveRecord::Base
  attr_accessible :role_name
   has_and_belongs_to_many :users

end

users_controller.rb Controller

class UsersController < ApplicationController
 # GET /users
  # GET /users.xml

#load_and_authorize_resource

  def index
   #  @user = User.find(params[:id])
    #@user.roles
    @users = User.all
    @roles = Role.all
    respond_to do |format|
      format.html # index.html.erb
  format.xml  { render :xml => @users }
end
  end

  # GET /users/1
  # GET /users/1.xml

 def show
    @user = User.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @user }
    end
  end

I can create a new user and assign a role to him. I can edit the user and change his role. and I can use show to see the individual users role.

The problem is with the index.html.erb file where I am trying to show All users and their roles.

Here is the code.

../views/users/index.html.erb

Listing users

<table>
  <tr>
     <th>ID</th>
    <th>Email</th>
    <th>Username</th>
    <th>Role    </th>
     <th>Role ID</th>>
  </tr>

  <% @users.each do |user| %>
    <tr>
     <td><%= user.id %></td>
         <%= current_user = user.id %>
      <td><%= user.email %></td>
      <td><%= user.username %></td>
     <td> <%= User.find(user.id).roles %> </td>
      <td><%= link_to 'Show', user %></td>
      <td><%= link_to 'Edit', edit_user_path(user) %></td>
      <td><%= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete %></td>
    </tr>
  <% end %>
</table>

<br />

This produces this output:

[email protected]    john     [#<Role id: 1, role_name: "basic_user", created_at: "2012-12-06 18:03:43", updated_at: "2012-12-06 18:03:43">] Show    Edit    Destroy

So it is giving me the entire Roles row instead of just the role_name.

I have tried User.find(user.id).roles.role_name, User.find(user.id).roles[1], and several other possibilities and cannot figure out how to just get the role name only to appear.

What is the proper syntax to get just the 'role_name' to display like:

[email protected]    john     basic_user        Show   Edit  Destroy

Thanks!

Upvotes: 1

Views: 881

Answers (1)

BvuRVKyUVlViVIc7
BvuRVKyUVlViVIc7

Reputation: 11811

you can just access it directly:

<td> <%= user.roles.collect(&:name).join(",") %> </td>

Upvotes: 3

Related Questions