在Active Admin中选择'has_many through'关联

问题描述 投票:2回答:1

例如,我见过几个同样的问题,对此问>]

Using HABTM or Has_many through with Active Admin

但是我仍在努力使事情顺利进行(在这一点上我已经尝试了多种方法)。

我的模型(用户模型的别名为'technical'有点复杂):

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  

尽管该表格似乎可以显示,但是所选技术人员在提交时未获得附件。在日志中,我得到一个错误“不允许的参数:analysis_type_technician_ids”。

我在其他相关的SO页面上尝试了多种建议来遵循此建议,但始终会遇到相同的问题,即某些性质的不允许参数。谁能指出我做错了什么? (顺便说一下,我正在使用Rails 4)

[我已经看到了一些相同的问题,例如通过Active Admin使用HABTM或Has_many,但我仍在努力工作(我在...

ruby-on-rails associations activeadmin has-many-through
1个回答
19
投票

通过has_and_belongs_to_manyhas_many关系管理关联不需要使用accepts_nested_attributes_for。这种形式输入正在管理与AnalysisType记录关联的技术员ID。定义允许的参数和格式如下所示要创建的关联。

© www.soinside.com 2019 - 2024. All rights reserved.