簡體   English   中英

在 Active Admin 中選擇“has_many through”關聯

[英]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_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


注意:更新答案以匹配評論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM