Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

ruby - Rails Activeadmin - custom association select box

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.

question from:https://stackoverflow.com/questions/15491789/rails-activeadmin-custom-association-select-box

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...