Rails Activeadmin - custom association select box

alexs333 picture alexs333 · Mar 19, 2013 · Viewed 32k times · Source

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.

Answer

HungryCoder picture HungryCoder · Mar 19, 2013

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