activeadmin has_many隐藏删除按钮

时间:2017-10-20 11:03:00

标签: ruby-on-rails activeadmin

我通过协会得到了一个has_many,如下所示:

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

和一个activeadmin页面:

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

我想删除&#39;删除按钮&#39;来自f.has_many,但我似乎无法使其发挥作用。我使用allow_destroy: false,但即使将其添加到accepts_nested_resources也无效。有谁知道如何让它发挥作用?

2 个答案:

答案 0 :(得分:2)

奇怪

从文档看起来不包括:allow_destroy是不具备该销毁选项的解决方案

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
  

:allow_destroy选项在嵌套表单的末尾添加一个复选框,允许在提交时删除子对象。确保在关联上设置allow_destroy:true以使用此选项。可以将allow_destroy与字符串或符号相关联,该字符串或符号对应于将被调用的子对象的方法的名称,或者与Proc对象相关联。 Proc对象接收子对象作为参数,并返回true或false。

还需要in some cases,包括accepts_nested_attributes_for :images, allow_destroy: true以包含此选项

我不知道如何解决这个问题,也许你应该在他们的github页面上发布一个问题?

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

答案 1 :(得分:0)

我为此使用了 css。

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