In my application everything works fine, but in my Active Admin backend I don't get my user roles displayed on screen.
I have two models "User" and "Roles":
class Role < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :roles_users
end
class User < ActiveRecord::Base
has_and_belongs_to_many :roles, :join_table => :roles_users
end
I get it to work in the rails console:
ruby-1.9.2-p290 :006 > user.roles
=> [#<Role id: 3, name: "Student">, #<Role id: 2, name: "Supervisor">]
ruby-1.9.2-p290 :007 > user.roles[0].name
=> "Student"
ruby-1.9.2-p290 :008 > user.roles[1].name
=> "Supervisor"
And I tried several ways of implementing this in Active Admin DSL (one of it):
ActiveAdmin.register User do
index do
column :email
column "Role" do |user|
user.roles.each do |p|
p.name
end
end
end
end
Could somebody please help me? How do I get it to work in DSL of Active Admin?
I haven't tested this myself, but I believe you need to return a string from the block in "column", so something like
column "Role" do |user|
user.roles.map({ |p| p.name }).join(' ')
end
might work.
That's the working code (in my case):
column "Role" do |user|
user.roles.map { |p| p.name }.join('<br />').html_safe
end
The Array map function: http://corelib.rubyonrails.org/classes/Array.html#M000427
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With