Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveAdmin - implement HABTM / multidimensional array in DSL

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?

like image 263
user930328 Avatar asked Sep 14 '11 16:09

user930328


2 Answers

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.

like image 169
Michael Hellein Avatar answered Oct 20 '22 08:10

Michael Hellein


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

like image 27
user930328 Avatar answered Oct 20 '22 09:10

user930328