ruby-on-rails - ActiveAdmin 表单不保存嵌套对象

标签 ruby-on-rails ruby-on-rails-4 activeadmin formtastic

在 Rails 4 中使用 ActiveAdmin,我有两个模型,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在下面代码的表单 View 中。这显示了一个添加附件的选项,它工作得很好。
 # 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

但是,当我提交表单时,文档对象被保存,但没有附件对象与它一起保存。据我所知,它应该创建与我在表单中添加的附件一样多的附件,并将它们的 document_id 属性传递给创建的文档 ID。不幸的是,这不会发生,在显示 View 中留下附件行“EMPTY”。我错过了什么吗?

提前致谢。

最佳答案

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

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

关于ruby-on-rails - ActiveAdmin 表单不保存嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20648081/

相关文章:

ruby-on-rails - rails : How to cancel current statements in rails console?

ruby-on-rails - 如何将json存储到rails中的数据库

ruby-on-rails - 重定向后 session 丢失

ruby-on-rails - Rails : In a controller, 向表单提交的参数添加一个值

ruby-on-rails - 在我对寄存器文件进行任何更改之前,关联的(动态)范围不起作用

ruby-on-rails - 列出所有关联作为事件管理中的链接

ruby-on-rails - Controller 定义中的脚手架不再有效吗?

ruby-on-rails - Rails报告花费时间渲染 View 时意味着什么?

ruby-on-rails - 类型错误 : no implicit conversion of Symbol into Integer

ruby-on-rails-3 - ActiveAdmin 自动完成下拉选择不起作用