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

以下を試してみてください。name は users テーブルの列で、role_name は roles テーブルの列です。

<%= 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 に答える