具有活动记录关联的simple_form嵌套复选框

时间:2018-09-06 14:36:54

标签: ruby-on-rails ruby simple-form

我正在使用simple_form做一个表单,我想使用我的模型Country做一个嵌套的复选框表单,例如:

Country has_many states

所以我想在每个国家/地区下面打印州协会

[] United States
  [] Alabama
  [] Chicago
  [] Washington
[] Colombia
  [] Cartagena
  [] Bogota
  [] Cali

类似的东西。

现在我正在使用它:

%label= "#{t('country.countries')}"
    = f.input :countries_id,
              as: :check_boxes,
              label: false,
              collection: (countries).sort_by(&:order_number),
              value_method: :id_class,
              input_html: { checked: true,
                            class: 'js-countries-report-card' }  

但是这仅打印国家名称,我如何在每个国家/地区下面打印协会?

谢谢。

编辑:

%label= "#{t('country.countries')}"
- countries.each do |country|   
    = f.input :countries_id,
              as: :check_boxes,
              label: false,
              collection: country,
              value_method: :id_class,
              input_html: { checked: true,
                            class: 'js-countries-report-card' }      
    - country.states.each do |state|
          = f.input :countries_id,
              as: :check_boxes,
              label: false,
              collection: state,
              value_method: :id_class,
              input_html: { checked: true,
                            class: 'js-countries-report-card' } 

0 个答案:

没有答案