Gregg
Gregg

Reputation: 125

activeadmin has_many hide remove button

I've got a has_many through association as in below:

class Room < ApplicationRecord
  has_many :room_options
  has_many :options, through: :room_options

  accepts_nested_attributes_for :room_options, allow_destroy: false
end

class RoomOption < ApplicationRecord
  belongs_to :room
  belongs_to :option
end

class Option < ApplicationRecord
  has_many :room_options
  has_many :rooms, through: :room_options
end

and an activeadmin page:

ActiveAdmin.register Room do
  permit_params :name, :guests_capacity, :description, :price, photos_attributes: [:id, :image, :is_primary, :_destroy]

  form(:html => { :multipart => true }) do |f|
    f.inputs do
      f.input :name
      f.input :guests_capacity
      f.input :description
      f.has_many :photos, allow_destroy: true do |photo|
        photo.input :image, as: :file, 
        hint: image_tag(photo.object.image_url(:thumb))
        photo.input :is_primary
      end

      Option.find_each { |option| f.object.room_options.build(option: option)}

      f.has_many :room_options, new_record: false, allow_destroy: false do |rof|
        rof.input :option_id, as: :hidden
        rof.input :has_option, as: :boolean, label: rof.object.option.name
      end

      f.input :price
    end
    f.actions
  end

end

I want to remove 'remove button' from f.has_many but I can't seem to make it work. I used allow_destroy: false but it doesn't work even adding it to accepts_nested_resources. Does anyone know how to make it work?

Upvotes: 2

Views: 2236

Answers (3)

Tran Light
Tran Light

Reputation: 1

Can resolve this problem like this:

form do |f|
    f.inputs "User Details"  do
        f.input :name
        f.input :description
        f.input :slug
        f.input :available_on, as: :datepicker ,datepicker_options:datepicker_options
        f.object.variants.each { |variant| variant.instance_eval('@new_record = false') }
        f.has_many :variants, new_record: false do |variants_form|
          variants_form.input :price
        end
    end

Upvotes: 0

nazar kuliyev
nazar kuliyev

Reputation: 1265

I use css for this.

  .has_many_container.room_options .has_many_remove {
    display: none;
  }

Upvotes: 0

fabOnReact
fabOnReact

Reputation: 5942

strange

from the documentation it looks like that not including :allow_destroy is the solution for not having that destroy option

ActiveAdmin.register Post do

  form do |f|
    f.inputs 'Details' do
      f.input :title
      f.input :published_at, label: 'Publish Post At'
    end
    f.inputs 'Content', :body
    f.inputs do
      f.has_many :categories, heading: 'Themes',
                              allow_destroy: true,
                              new_record: false do |a|
        a.input :title
      end
    end
    f.inputs do
      f.has_many :taggings, sortable: :position, sortable_start: 1 do |t|
        t.input :tag
      end
    end
    f.inputs do
      f.has_many :comment,
                 new_record: 'Leave Comment',
                 allow_destroy: -> { |c| c.author?(current_admin_user) } do |b|
        b.input :body
      end
    end
    f.actions
  end

end

The :allow_destroy option adds a checkbox to the end of the nested form allowing removal of the child object upon submission. Be sure to set allow_destroy: true on the association to use this option. It is possible to associate :allow_destroy with a string or a symbol, corresponding to the name of a child object’s method that will get called, or with a Proc object. The Proc object receives the child object as a parameter and should return either true or false.

also in some cases was necessary including the accepts_nested_attributes_for :images, allow_destroy: true to include this option

I don't know how to solve this, maybe you should post an issue in their github page?

https://github.com/activeadmin/activeadmin/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20destroy

Upvotes: 4

Related Questions