sam*_*ers 7 ruby-on-rails nested-attributes activeadmin
我无法弄清楚如何在Active Admin中使用嵌套资源输入助手,以允许我更新"父"记录的相关记录的值.
我正在尝试生成更新的模型是这样的:
class Page < ActiveRecord::Base
has_many :page_attributes
accepts_nested_attributes_for :page_attributes, allow_destroy: true
end
Run Code Online (Sandbox Code Playgroud)
哪里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
Run Code Online (Sandbox Code Playgroud)
但是当我调用http://localhost:3000/admin/pages/2/edit并更改现有属性的值时(或者当我检查"删除"复选框时),会发生什么,PageAttribute即创建模型的新记录并保持现有关联不变.
我通读了有关嵌套资源的Active Admin文档,以及一堆SO帖子,但无法弄清楚我做错了什么:(
sam*_*ers 23
我意识到我做错了什么 - 我有点过分思考了.我不知道当您允许强参数时,您还必须允许:id您尝试更新的关联记录上的参数.我有点假设Rails魔术会照顾它.
因此,如果您更改permit_params呼叫以改为:
permit_params page_attributes_attributes: [:id, :key, :value, :_destroy => true]
Run Code Online (Sandbox Code Playgroud)
事实上,这就是Active Admin Github wiki上的强参数部分所说的,我应该注意为什么它以这种方式设置.
| 归档时间: |
|
| 查看次数: |
6756 次 |
| 最近记录: |