I need to pre-check multiple checkboxes in form for habtm model in ActiveAdmin while creating new object. Array with ids of nested model taken from array in third-party model database record. My current configuration:
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
Array with ids of pages that should be prechecked:
Setting.find_by_name("defined_pages_ids").value.split(',').map(&:to_i) # [1,2,3,4]
What solution do i need to implement pre-checking?
You need to override creating a new instance of Hotel model, and prepopulate needed data
controller do
def new
@hotel = Hotel.new
@hotel.pages << Page.all
end
end