预先检查Rails ActiveAdmin中的多个复选框

问题描述 投票:0回答:1

我需要在创建新对象时预先检查ActiveAdmin中habtm模型的多个复选框。具有嵌套模型的ID的数组,取自第三方模型数据库记录中的数组。我目前的配置:

ActiveAdmin.register Hotel do
  permit_params page_ids:[]
  ...
  form do |f|
    ...
    f.inputs 'Pages' do
      f.input :pages, as: :check_boxes, collection: Page.order('position asc')
    end
    f.actions
  end
end

class Hotel < ApplicationRecord
  has_and_belongs_to_many :pages
  accepts_nested_attributes_for :pages
  ...
end

class Page < ApplicationRecord
  has_and_belongs_to_many :hotels
  ...
end

包含应该预先检查的页面ID的数组:

Setting.find_by_name("defined_pages_ids").value.split(',').map(&:to_i) # [1,2,3,4]

我需要什么解决方案来实施预检查?

ruby-on-rails activeadmin formtastic
1个回答
0
投票

您需要覆盖创建酒店模型的新实例,并预填充所需的数据

  controller do

    def new
      @hotel = Hotel.new
      @hotel.pages << Page.all
    end

end
© www.soinside.com 2019 - 2024. All rights reserved.