activeadmin has_many 隐藏删除按钮
activeadmin has_many hide remove button
我有一个 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
我想从 f.has_many
中删除 'remove button',但我似乎无法让它工作。我使用了 allow_destroy: false
,但即使将它添加到 accepts_nested_resources
也不起作用。有谁知道如何让它工作?
奇怪
从文档看来,不包括 :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
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.
还需要 in some cases 包括 accepts_nested_attributes_for :images, allow_destroy: true
以包括此选项
我不知道如何解决这个问题,也许你应该 post 在他们的 github 页面上提出一个问题?
https://github.com/activeadmin/activeadmin/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20destroy
我为此使用 css。
.has_many_container.room_options .has_many_remove {
display: none;
}
我有一个 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
我想从 f.has_many
中删除 'remove button',但我似乎无法让它工作。我使用了 allow_destroy: false
,但即使将它添加到 accepts_nested_resources
也不起作用。有谁知道如何让它工作?
奇怪
从文档看来,不包括 :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
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.
还需要 in some cases 包括 accepts_nested_attributes_for :images, allow_destroy: true
以包括此选项
我不知道如何解决这个问题,也许你应该 post 在他们的 github 页面上提出一个问题?
https://github.com/activeadmin/activeadmin/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20destroy
我为此使用 css。
.has_many_container.room_options .has_many_remove {
display: none;
}