42

In my Rails application, I have the following model:

class Idea < ActiveRecord::Base
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :ideas
end

I am creating ActiveAdmin CRUD for my Idea model with the custom form that looks something like that looks something like that:

form do |f|
  f.inputs do
    f.input :member
    f.input :description
  end
end

The requirement is to have the custom text for a content of the member association, i.e "#{last_name}, #{first_name}". Is it possible to customize my member select box to achieve it?

Any help will be appreciated.

HungryCoder
  • 7,506
  • 1
  • 38
  • 51
alexs333
  • 12,143
  • 11
  • 51
  • 89

2 Answers2

94

Yes, that is possible. I assume you want to use a DropDown list box for members to select a user from User model.

form do |f|
  f.inputs do
    f.input :user_id, :label => 'Member', :as => :select, :collection => User.all.map{|u| ["#{u.last_name}, #{u.first_name}", u.id]}
    f.input :description
  end
end
HungryCoder
  • 7,506
  • 1
  • 38
  • 51
  • 2
    This type of map in collection don't work for me... the activeadmin always shows my u.name instead a u.cod like I set... my collection: User.all.map{|u| [u.cod, u.id]} – squiter Mar 28 '14 at 12:46
0

For Active Admin You have to pass it as a collection of Hashes. Key in hash will be the text which you want to display and value will be the attribute id.

For Example:

f.input :user_id, :label => 'Member', :as => :select, :collection => User.all.map{|u| ["#{u.last_name}, #{u.first_name}", u.id]}.to_h

collection: [{name1: 1}, {name2: 2}, {name3: 3}]

Note: I have added to_h at end of the map which will convert a collection of arrays into a collection of hashes.