简体   繁体   English

在 Active Admin 中选择“has_many through”关联

[英]Selecting 'has_many through' associations in Active Admin

I've seen several questions asked about this along the same vein, eg我已经看到了几个类似的问题,例如

Using HABTM or Has_many through with Active Admin 通过 Active Admin 使用 HABTM 或 Has_many

but I'm still struggling to get things to work (I've tried multiple ways at this point).但我仍在努力让事情工作(此时我已经尝试了多种方法)。

My models (slightly complicated by the 'technician' alias for the user model):我的模型(由于用户模型的“技术员”别名而稍微复杂了一些):

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

I have registered an ActiveAdmin model for the AnalysisType model and want to be able to select (already created) Technicians to associate with that AnalysisType in a dropdown/checkbox.我已经为 AnalysisType 模型注册了一个 ActiveAdmin 模型,并且希望能够在下拉列表/复选框中选择(已经创建)与该 AnalysisType 关联的技术人员。 My ActiveAdmin setup currently looks like:我的 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  

Whilst the form seems to display okay, the selected technician does not get attached upon submitting.虽然表单似乎显示正常,但所选的技术人员在提交时并未附加。 In the logs I'm getting an error 'Unpermitted parameters: analysis_type_technician_ids'.在日志中,我收到一个错误“不允许的参数:analyze_type_technician_ids”。

I've tried multiple ways of doing this following advice in other related SO pages but am always coming up against the same issue, ie unpermitted parameterd of some nature.我已经尝试了多种方法来按照其他相关 SO 页面中的建议执行此操作,但总是遇到相同的问题,即某种性质的未经许可的参数化。 Can anyone point out what I am doing wrong?谁能指出我做错了什么? (I'm using Rails 4 by the way) (顺便说一下,我正在使用 Rails 4)

Managing the association via has_and_belongs_to_many or has_many relations does not require the use of accepts_nested_attributes_for .通过has_and_belongs_to_manyhas_many关系管理关联不需要使用accepts_nested_attributes_for This type of form input is managing the Technician IDs associated with the AnalysisType record.这种类型的表单输入管理与 AnalysisType 记录关联的技术员 ID。 Defining your permitted parameters and form like the following should allow those associations to be created.像下面这样定义允许的参数和表单应该允许创建这些关联。

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

In the case where the creation of new Technician records is required that is when the accepts_nested_attributes_for would be used.在需要创建新技术员记录的情况下,将使用accepts_nested_attributes_for


Note : Updated answer to match comments.注意:更新答案以匹配评论。

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

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