makeapp
makeapp

Reputation: 181

how to customize id for collection_check_boxes

I am using rails4 and bootstrap_form.

When I write

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

the html is like

<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>

Is there a way to customize id to something like helps_resume, helps_interview, where "resume" and "interview" is in People.all_helps?

Also what is the argument :id for? How can I use this value method?

Upvotes: 2

Views: 1262

Answers (1)

Sebasti&#225;n Palma
Sebasti&#225;n Palma

Reputation: 33471

You can see the collection_check_boxes is defined as:

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

So you can add specify the options argument as an empty hash, and then add any html option, like id in your case. So this should work (should):

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

You need to open a block, print the label, corresponding to the block, inside the label open a block again and print the input, being there you can edit the html options as needed:

= 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 }

For further info you can inspect block.

Upvotes: 4

Related Questions