gpt4 book ai didi

ruby-on-rails - 在 Active Admin 中选择 'has_many through' 关联

转载 作者:行者123 更新时间:2023-12-01 16:00:13 26 4
gpt4 key购买 nike

我已经看到了几个关于这个问题的问题,例如

Using HABTM or Has_many through with Active Admin

但我仍在努力让事情正常进行(此时我已经尝试了多种方法)。

我的模型(由于用户模型的“技术人员”别名而略微复杂):

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

虽然表格似乎显示正常,但选定的技术人员在提交时并未附加。在日志中,我收到错误“Unpermitted parameters: analysis_type_technician_ids”。

我已经按照其他相关 SO 页面中的建议尝试了多种方法来执行此操作,但我总是遇到同样的问题,即某种性质的未经许可的参数化。谁能指出我做错了什么? (顺便说一句,我正在使用 Rails 4)

最佳答案

通过has_and_belongs_to_manyhas_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


注意:已更新答案以匹配评论。

关于ruby-on-rails - 在 Active Admin 中选择 'has_many through' 关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27934223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com