ActiveAdmin表单不保存嵌套对象

使用ActiveAdmin和Rails 4,我有两个模型, DocumentAttachment ,它们之间有一对多的关系。

 # models/document.rb class Document < ActiveRecord::Base has_many :attachments accepts_nested_attributes_for :attachments end # models/attachment.rb class Attachment < ActiveRecord::Base belongs_to :document end 

我注册了模型,并为每个模型中的所有字段包含了permit_params 。 现在我在下面的代码中使用了表单视图中的has_many 。 这显示了添加附件的选项,它工作得很好。

  # admin/document.rb ActiveAdmin.register Document do permit_params :title, :description, :date, :category_id show do |doc| attributes_table do row :title row :description row :attachments do doc.attachments.map(&:document_path).join("
").html_safe end end end form do |f| f.inputs "Details" do f.input :title f.input :description f.input :category f.has_many :attachments, :allow_destroy => true do |cf| cf.input :document_path # which is a field in the Attachment model end end f.actions end end

但是,当我提交表单时,会保存文档对象,但不会保存附件对象。 据我所知,它应该创建我在表单中添加的尽可能多的附件,并将其创建的文档ID传递给document_id属性。 不幸的是,这不会发生在显示视图中将附件行“EMPTY”。 我错过了什么吗?

提前致谢。

你忘了允许attachments_attributes。 要将accepts_nested_attribute_for与强参数一起使用,您需要指定哪些嵌套属性应列入白名单。

更多信息http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html