我正在使用rails4和bootstrap_form。
当我写作时
= f.collection_check_boxes :helpability, People.all_helps, :id, :name
html 就像
<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>
有没有办法将 id 自定义为 helps_resume
、helps_interview
之类的内容,其中“resume”和“interview”位于 People.all_helps 中?
参数 :id
的用途是什么?我该如何使用这个值方法?
最佳答案
您可以看到collection_check_boxes
定义为:
collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
因此,您可以添加将选项参数指定为空哈希,然后添加任何 html 选项,例如您的情况下的 id 。所以这应该有效(应该):
= f.collection_check_boxes :helpability, People.all_helps, :id, :name, {}, id: 'helps_resume'
您需要打开一个 block ,打印与该 block 相对应的标签,在标签内再次打开一个 block 并打印输入,在那里您可以根据需要编辑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
。
关于ruby-on-rails - 如何自定义collection_check_boxes的id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47502221/