【问题标题】:How to implement a many-to-many in ActiveAdmin?如何在 ActiveAdmin 中实现多对多?
【发布时间】:2016-05-05 11:42:14
【问题描述】:

有一个多对多:

class Employee < ActiveRecord::Base
    has_many :employees_and_positions
    has_many :employees_positions, through: :employees_and_positions
end

class EmployeesAndPosition < ActiveRecord::Base
    belongs_to :employee
    belongs_to :employees_position
end

class EmployeesPosition < ActiveRecord::Base
    has_many :employees_and_positions
    has_many :employees, through: :employees_and_positions
end

添加员工时如何在表单中实现选择(check_boxes)职位? 我写了这个变种:

f.inputs 'Communications' do
    f.input :employees_positions, as: :check_boxes
end

它在表单中显示职位列表,但不会将任何内容保存到表中(employees_and_positions)。 如何解决?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 activeadmin


    【解决方案1】:

    假设您有一个employee,您可以使用employee.employees_position_ids 引用employees_positions 关联的ID。因此,您可以使用每个EmployeesPosition 的复选框来批量分配预先存在的EmployeesPosition 对象,但您需要使用employee_position_ids 属性"

    = f.input :employee_position_ids, as: :check_boxes, collection: EmployeesPosition.all
    

    此外,请确保您已将活动管理资源中的 employee_position_ids 参数列入白名单:

    ActiveAdmin.register Employee do
      permit_params employee_position_ids: []
    end
    

    http://activeadmin.info/docs/2-resource-customization.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-09
      • 2012-04-05
      • 1970-01-01
      • 2022-08-17
      • 1970-01-01
      • 2012-10-11
      • 2019-02-02
      • 2016-11-01
      相关资源
      最近更新 更多