如何为collection_check_boxes自定义id

时间:2017-11-26 23:41:31

标签: ruby-on-rails ruby-on-rails-4

我正在使用rails4和bootstrap_form。

写作时

30
22
64
58
58

html就像

= f.collection_check_boxes :helpability, People.all_helps, :id, :name

有没有办法将ID定制为<div class="checkbox"> <label for="people_helpability_resume"> <input type="checkbox" value="resume" name="people[helpability][]" id="people_helpability_resume" /> RESUME </label> </div> <div class="checkbox"> <label for="people_helpability_interview"> <input type="checkbox" value="interview" name="people[helpability][]" id="people_helpability_interview" /> INTERVIEW </label> </div> helps_resume,其中“resume”和“interview”位于People.all_helps?

论证helps_interview的含义是什么?我该如何使用这个值方法?

1 个答案:

答案 0 :(得分:1)

您可以看到collection_check_boxes定义为:

collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block)

因此,您可以将options参数指定为空哈希,然后添加任何html选项,例如id。所以这应该工作(应该):

= f.collection_check_boxes :helpability, People.all_helps, :id, :name, {}, id: 'helps_resume'

你需要打开一个块,打印标签,对应于块,在标签内再次打开一个块并打印输入,在那里你可以根据需要编辑html选项:

= form.collection_check_boxes :helpability, People.all_helps, :id, :name do |block| 
  = block.label { block.check_box id: "#{block.object.name}-id" } 
  = block.label { block.object.name }

有关详细信息,您可以查看block