[英]Selecting 'has_many through' associations in Active Admin
我已经看到了几个类似的问题,例如
通过 Active Admin 使用 HABTM 或 Has_many
但我仍在努力让事情工作(此时我已经尝试了多种方法)。
我的模型(由于用户模型的“技术员”别名而稍微复杂了一些):
class AnalysisType < ActiveRecord::Base
has_many :analysis_type_technicians, :dependent => :destroy
has_many :technicians, :class_name => 'User', :through => :analysis_type_technicians
accepts_nested_attributes_for :analysis_type_technicians, allow_destroy: true
end
class User < ActiveRecord::Base
has_many :analysis_type_technicians, :foreign_key => 'technician_id', :dependent => :destroy
has_many :analysis_types, :through => :analysis_type_technicians
end
class AnalysisTypeTechnician < ActiveRecord::Base
belongs_to :analysis_type, :class_name => 'AnalysisType', :foreign_key => 'analysis_type_id'
belongs_to :technician, :class_name => 'User', :foreign_key => 'technician_id'
end
我已经为 AnalysisType 模型注册了一个 ActiveAdmin 模型,并且希望能够在下拉列表/复选框中选择(已经创建)与该 AnalysisType 关联的技术人员。 我的 ActiveAdmin 设置目前看起来像:
ActiveAdmin.register AnalysisType do
form do |f|
f.input :analysis_type_technicians, as: :check_boxes, :collection => User.all.map{ |tech| [tech.surname, tech.id] }
f.actions
end
permit_params do
permitted = [:name, :description, :instrumentID, analysis_type_technicians_attributes: [:technician_id] ]
permitted
end
end
虽然表单似乎显示正常,但所选的技术人员在提交时并未附加。 在日志中,我收到一个错误“不允许的参数:analyze_type_technician_ids”。
我已经尝试了多种方法来按照其他相关 SO 页面中的建议执行此操作,但总是遇到相同的问题,即某种性质的未经许可的参数化。 谁能指出我做错了什么? (顺便说一下,我正在使用 Rails 4)
通过has_and_belongs_to_many
或has_many
关系管理关联不需要使用accepts_nested_attributes_for
。 这种类型的表单输入管理与 AnalysisType 记录关联的技术员 ID。 像下面这样定义允许的参数和表单应该允许创建这些关联。
ActiveAdmin.register AnalysisType do
form do |f|
f.input :technicians, as: :check_boxes, collection: User.all.map { |tech| [tech.surname, tech.id] }
f.actions
end
permit_params :name, :description, :instrumentID, technician_ids: []
end
在需要创建新技术员记录的情况下,将使用accepts_nested_attributes_for
。
注意:更新答案以匹配评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.