activeadmin: adding delete for a nested resource

Seoman picture Seoman · Jan 24, 2012 · Viewed 14.3k times · Source

I have a infrastructure object composed for many datacenters. In the apps/admin/infrastructures.rb I have the following code:

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

I can add datacenters with no problems but I don't know how I can delete it from infrastructure form.

Answer

tomek picture tomek · Jun 25, 2012

Sep 2017 Update:

Rails 5.1.4, ActiveAdmin 1.0.0

Append :id and _destroy in permit_params along with other attributes from the model e.g. :name in your case. Then provide the :allow_destroy option in f.has_many too. Other requirements remain the same; like adding allow_destroy: true in accepts_nested_attributes_for.

Final look:

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

ActiveAdmin Reference


This worked for me:

     i.input :_destroy, as: :boolean

and in the Model remember to add :allow_destroy :

     accepts_nested_attributes_for :images, allow_destroy: true