Ruby-on-rails Rails Activeadmin - 自定义关联选择框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15491789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:39:15  来源:igfitidea点击:

Rails Activeadmin - custom association select box

ruby-on-railsrubyactiveadmin

提问by alexs333

In my Rails application, I have the following model:

在我的 Rails 应用程序中,我有以下模型:

class Idea < ActiveRecord::Base
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :ideas
end

I am creating ActiveAdminCRUD for my Ideamodel with the custom form that looks something like that looks something like that:

我正在使用自定义表单为我的模型创建ActiveAdminCRUD Idea,该表单看起来像这样:

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?

要求是具有成员协会内容的自定义文本,即"#{last_name}, #{first_name}". 是否可以自定义我的会员选择框来实现它?

Any help will be appreciated.

任何帮助将不胜感激。

回答by HungryCoder

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

是的,这是可能的。我假设您想使用下拉列表框membersUser模型中选择用户。

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