我有一个为许多数据中心组成的基础设施对象。在 apps/admin/infrastructures.rb 我有以下代码:
form do |f|
f.inputs "Infrastructure details" do
f.input :name
f.has_many :datacenters do |datacenter_form|
datacenter_form.input :name
end
end
f.buttons
end
我可以毫无问题地添加数据中心,但我不知道如何从基础设施表单中删除它。
最佳答案
2017 年 9 月更新:
Rails 5.1.4,ActiveAdmin 1.0.0
在 permit_params
中附加 :id
和 _destroy
以及模型的其他属性,例如:name
在你的情况下。然后在 f.has_many
中也提供 :allow_destroy
选项。其他要求不变;比如在 accepts_nested_attributes_for
中添加 allow_destroy: true
。
最后的样子:
ActiveAdmin.register Infrastructure do
permit_params :name, datacenters_attributes: [:id, :_destroy, :name]
form do |f|
f.inputs "Infrastructure details" do
f.input :name
f.has_many :datacenters, heading: false,
allow_destroy: true,
new_record: false do |datacenter_form|
datacenter_form.input :name
end
end
f.buttons
end
end
这对我有用:
i.input :_destroy, as: :boolean
并且在模型中记得添加 :allow_destroy :
accepts_nested_attributes_for :images, allow_destroy: true
关于ruby-on-rails - activeadmin:为嵌套资源添加删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8980714/