activeadmin: adding delete for a nested resource

前端 未结 6 1896
我在风中等你
我在风中等你 2021-02-12 10:42

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 \"Infrastruc         


        
6条回答
  •  无人共我
    2021-02-12 11:00

    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
    

提交回复
热议问题