【问题标题】:Nested has_many resource form with Active Admin doesn't do an update具有 Active Admin 的嵌套 has_many 资源表单不进行更新
【发布时间】:2014-04-03 05:11:18
【问题描述】:

我不知道如何使用 Active Admin 中的嵌套资源输入助手来更新“父”记录的关联记录的值。

我试图为其生成更新的模型是这样的:

class Page < ActiveRecord::Base
  has_many :page_attributes
  accepts_nested_attributes_for :page_attributes, allow_destroy: true
end

其中PageAttribute有两个属性,:key:value

ActiveAdmin 模型是:

ActiveAdmin.register Page do
  permit_params page_attributes_attributes: [:key, :value, :_destroy => true]

  form do |f|
    f.inputs do
      f.has_many :page_attributes, allow_destroy: true, heading: 'Parts' do |page_part|
    page_part.input :key
    page_part.input :value
      end
    end

    f.actions
  end
end

但是当我调用http://localhost:3000/admin/pages/2/edit 并更改现有属性的值时(或者当我选中删除复选框时),会发生一个新的PageAttribute 模型的记录和现有的关联保持不变。

我阅读了Active Admin documentation on nested resources 和一堆 SO 帖子,但不知道我做错了什么:(

【问题讨论】:

    标签: ruby-on-rails activeadmin nested-attributes


    【解决方案1】:

    我意识到我做错了什么——我有点想多了。我不知道当您允许强参数时,您还必须在您尝试更新的关联记录上允许 :id 参数。我有点假设 Rails 的魔法会解决这个问题。

    因此,如果您将 permit_params 调用改为这样说,它会起作用:

    permit_params page_attributes_attributes: [:id, :key, :value, :_destroy => true]
    

    其实the Strong Parameters section on the Active Admin Github wiki就是这么说的,我应该注意一下为什么会这样设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-02
      • 1970-01-01
      • 2016-05-29
      • 2012-06-09
      • 2012-04-21
      • 1970-01-01
      相关资源
      最近更新 更多