2

我正在尝试使用 wice_grid gem 在网格表中获取用户的角色

user has_many :roles, :through => :assignments

role has_many :users, :through => :assignments

在 users_controller.rb 中

@users_grid = initialize_grid(User,
                                  :include => [:assignments, :roles])

为了使用户的角色出现,我应该在视图中写什么,我无法从文档中获取它,所以需要帮助,请问如何在视图中编写它?

4

1 回答 1

2

只需尝试以下内容。让名称是用户表中的一列,角色名称是角色表中的一列。

<%= grid(@users_grid) do |g|
  # Here I have defined the column name. Column names are defined with parameter ':name' and the ':attribute' defines which column to map in the users table for this column.
  g.column :name => 'User Name', :attribute => 'name' do |user|  # primary table
    link_to(user.name, user_path(user))
  end

  # Regarding join tables or associations, we need to specify the model name as here, I have defined model: 'Role'.
  g.column name: 'Having Roles', attribute: 'role_name', model: 'Role' do |user|
    # here we are using the associations showing the data for the joints table.
    user.roles.collect{|role| role.role_name}.to_sentence
  end

  g.column do |user|
    link_to('Edit', edit_user_path(user))
  end
end%>

您可以查看示例示例的代码https://github.com/leikind/wice_grid_testbed

于 2013-07-06T17:11:53.550 回答