Skip to content

Make a grouping checkboxes.

Masamoto Miyata edited this page Aug 10, 2022 · 9 revisions
# You must turn on `multiple` flag to permit this param for an array.
config.form.item(:block, :sales_type_ids, multiple: true) do |form, field|
  tag.div class: 'wrapper' do
    # Need when submitted with empty.
    concat(form.hidden_field(:sales_type_ids, value: '', multiple: field.multiple))

    # Group iteration
    SalesTypeCategory.eager_load(:sales_types).each do |category|
      concat(
        tag.div(class: 'category mb-3') do
          concat(
            tag.div(category.name, class: 'category_name font-weight-bold border-bottom')
          )
          concat(
            tag.div(class: 'items py-2') do

              # Item iteration
              category.sales_types.each do |sales_type|
                id = "#{field.name}_#{sales_type.id}"
                concat(
                  tag.div(class: 'form-check form-check-inline', style: 'width: 390px;') do
                    # This is a view helper provided by dynamic_scaffold.
                    # https://github.com/gomo/dynamic_scaffold/blob/e60940798fbd6dead111e5823e34a9d2520677bb/app/helpers/dynamic_scaffold_helper.rb#L18-L32
                    render_check_box(form, field, sales_type.id, sales_type.name, { class: 'form-check-input' }, class: 'form-check-label')
                  end
                )
              end
            end
          )
        end
      )
    end
  end
end