ActiveAdmin表单未保存嵌套对象 [英] ActiveAdmin Form not saving nested object

查看:58
本文介绍了ActiveAdmin表单未保存嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ActiveAdmin和Rails 4,我有两个模型,分别是 Document Attachment

Using ActiveAdmin with Rails 4, I have two models, Document and Attachment with a one-to-many relationship between them.

# 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

I registered the models and included permit_params for all the fields in each. Now I used has_many in the form view in the below code. This shows an option to add Attachments and it work just fine.

 # 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("<br />").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留在了显示视图中。我想念什么吗?

However, when I submit the form, the document object is saved but no attachment objects are saved with it. As much as I understand it should create as many attachments I added in the form and pass in their document_id attribute the created document ID. Unfortunately this is not happening leaving the Attachment row "EMPTY" in the show view. Am I missing something?

预先感谢。

推荐答案

您忘记了允许attachments_attributes。
为了在带有Strong参数的情况下使用accepts_nested_attribute_for,您将需要指定将哪些嵌套属性列入白名单。

You forgot to permit attachments_attributes. In order to use accepts_nested_attribute_for with Strong Parameters, you will need to specify which nested attributes should be whitelisted.

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

这篇关于ActiveAdmin表单未保存嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆