24

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.

6 Answers 6

50

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
Sign up to request clarification or add additional context in comments.

3 Comments

With Rails 4, it only worked for me once I permitted :_destroy through my strong parameters: permit_params ..., nested_model_attributes: [:_destroy, ... ]
I'd forgotten the allow_destroy: true option on accepts_nested_attributes_for
I thought permit_params was enough, thank you! none of the other answers mention this.
14

Solved adding the following line:

datacenter_form.input :_destroy, :as => :boolean, :required => false, :label => 'Remove'

The code looks like:

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

4 Comments

this works, but it just seems sloppy. Is there a way to add the Delete button that active admin has when you add a new nested resource to existing resources on an update?
Agreed. Why doesn't the delete button appear for persisted records?
Agreed is there anyway to get a button?
Yes, has anyone found a solution to this?
5

If you cant destroy the object nested. You need to put :_destroy in your app/admin/object.rb permit_params

permit_params :id,:name, :cod, :_destroy

1 Comment

In case of nested attributes needed to be destroyed, the :_destroy param should be in the nested_attributes attributes. => ( permit_params :attr, :attr, <nestedattr>_attributes: [:attr, :attr, :_destroy]
1

I hope this will be helpful (I've changed my code to suit your example, so I hope there are no typos here):

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

      f.has_many :datacenters do |datacenter_form|
        datacenter_form.inputs :datacenters do
          datacenter_form.input :name
        end
        datacenter_form.buttons do
          link_to "Delete", admin_datacenter_path(datacenter_form.object), method: "delete", class: "button" unless datacenter_form.object.new_record?
        end
      end
    end
    f.buttons
  end

and the controller method should be defined in datacenters.rb

controller do
    def destroy
      @datacenter = Datacenter.find(params[:id])
      @datacenter.destroy
      redirect_to edit_admin_retailer_path(@datacenter.infrastructure)
    end
  end

Comments

0

This should work:

datacenter_form.label :_delete
datacenter_form.check_box :_delete

This adds a checkbox for each nested object which will delete the object if checked.

1 Comment

Thanks for the answer. I'm getting this error: "undefined method `_delete' for #<Datacenter:0x000000051d3170>"
0

Don't forget to add the following to your parent model

has_many :child_name, :dependent => :destroy

1 Comment

Please be more specific with your answer. Provide an explanation of what this does and how the code snippet answers the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.