简体   繁体   English

如何通过activeadmin形式的关系为has_many添加multiselect

[英]How to add multiselect for has_many through relationship in activeadmin form

I have two models Degree and College connected many_to_many connection via Discipline table. 我有两个模型Degree和College通过Discipline表连接many_to_many连接。

class Degree < ActiveRecord::Base
  has_many :disciplines
  has_many :colleges, :through => :disciplines
end 

class Discipline < ActiveRecord::Base
  belongs_to :college
  belongs_to :degree
end


class College < ActiveRecord::Base
  has_many :disciplines
  has_many :degrees, :through => :disciplines
end

I want to display multiple select (or checkboxes) with a degrees on the college new/update forms. 我想在学院新/更新表格上显示多个具有学位的选择(或复选框)。 How to do that? 怎么做?

In the College ActiveAdmin resource you can use the has_many method in the form block: 在College ActiveAdmin资源中,您可以在表单块中使用has_many方法:

ActiveAdmin.register College do
    #...

    form do
      #...
      f.has_many :disciplines do |df|
        df.input :degree
      end
      #...
    end
    #...
end

This will be a multiselect select input by default. 默认情况下,这将是多选选择输入。 Find out more: https://github.com/activeadmin/activeadmin/blob/master/docs/5-forms.md#nested-resources 了解更多信息: https//github.com/activeadmin/activeadmin/blob/master/docs/5-forms.md#nested-resources

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM