Col*_*bri 5 ruby-on-rails activeadmin ruby-on-rails-4
有很多对很多:
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
Run Code Online (Sandbox Code Playgroud)
添加员工时如何在表单中实现选择(check_boxes)职位?我写了这个变体:
f.inputs 'Communications' do
f.input :employees_positions, as: :check_boxes
end
Run Code Online (Sandbox Code Playgroud)
它在表单中显示职位列表,但不将任何内容保存到表中(employees_and_positions)。怎么修?
假设您有一个employee,则可以使用来引用employees_positions关联的ID employee.employees_position_ids。因此,您可以EmployeesPosition使用每个复选框的check_box 批量分配先前存在的对象EmployeesPosition,但是您需要使用employee_position_ids属性“
= f.input :employee_position_ids, as: :check_boxes, collection: EmployeesPosition.all
Run Code Online (Sandbox Code Playgroud)
另外,请确保已将employee_position_ids有效管理资源中的参数列入白名单:
ActiveAdmin.register Employee do
permit_params employee_position_ids: []
end
Run Code Online (Sandbox Code Playgroud)
http://activeadmin.info/docs/2-resource-customization.html
| 归档时间: |
|
| 查看次数: |
1294 次 |
| 最近记录: |