activeadmin:为嵌套资源添加删除

Seo*_*man 21 ruby ruby-on-rails activeadmin

我有一个为许多数据中心组成的基础结构对象.在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
Run Code Online (Sandbox Code Playgroud)

我可以添加没有问题的数据中心,但我不知道如何从基础架构表单中删除它.

tom*_*mek 39

2017年9月更新:

Rails 5.1.4,ActiveAdmin 1.0.0

追加:id_destroypermit_params与从模型如其他属性沿着:name你的情况.然后提供:allow_destroy选项f.has_many.其他要求保持不变; 像加allow_destroy: trueaccepts_nested_attributes_for.

最后看:

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
Run Code Online (Sandbox Code Playgroud)

ActiveAdmin参考


这对我有用:

     i.input :_destroy, as: :boolean
Run Code Online (Sandbox Code Playgroud)

并在模型中记得添加:allow_destroy:

     accepts_nested_attributes_for :images, allow_destroy: true
Run Code Online (Sandbox Code Playgroud)

  • 我忘记了`accepts_nested_attributes_for`上的`allow_destroy:true`选项 (3认同)
  • 使用Rails 4,只有通过我的强参数允许`:_destroy`时它才对我有用:`permit_params ...,nested_model_attributes:[:_ destroy,...]` (2认同)

Seo*_*man 14

解决了添加以下行:

datacenter_form.input :_destroy, :as => :boolean, :required => false, :label => 'Remove'
Run Code Online (Sandbox Code Playgroud)

代码如下:

form do |f|
  f.inputs "Infrastructure details" do
    f.input :name

    f.has_many :datacenters do |datacenter_form|
      datacenter_form.input :name
      datacenter_form.input :_destroy, :as => :boolean, :required => false, :label => 'Remove'
    end
  end
  f.buttons
end
Run Code Online (Sandbox Code Playgroud)

  • 这很有效,但看起来很草率.有没有办法在更新时向现有资源添加新的嵌套资源时添加活动管理员具有的"删除"按钮? (7认同)

小智 5

如果不能破坏嵌套的对象。您需要将:_destroy放入您的app / admin / object.rb permit_params中

permit_params :id,:name, :cod, :_destroy
Run Code Online (Sandbox Code Playgroud)