# Collection of Check Boxes and Labels

**URL:** <https://www.ruby-forum.com/t/collection-of-check-boxes-and-labels/146841>\
**Category:** Rails\
**Created:** [August 26, 2008, 6:18am UTC](https://www.ruby-forum.com/t/collection-of-check-boxes-and-labels/146841 "2008-08-26T06:18:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [August 26, 2008, 6:18am UTC](https://www.ruby-forum.com/t/collection-of-check-boxes-and-labels/146841/1 "2008-08-26T06:18:58Z")

</div>

So I have a collection of books. I want users to be able to check which  
of the books they are interested in. I want the name of the book next  
to the check box to be clickable so that it is easy to select.

In my view I have \<%= check\_box\_tag “books[]”, book.id %\>\<%= book.name  
%\> inside of an iteration.

This produces HTML of: Hamlet

I want to use a label and I know the “for” property in the label needs  
to refer to the id of the input tag. However, my id for the input tag  
is “books[]” for all books.

This has the benefit of creating a collection in params[:books] but I  
can’t figure out how to set the label.

I looked online and found things about labels and checkboxes but nothing  
specific to this scenario.

Any guidance would be much appreciated.

Cheers,

John

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [August 26, 2008, 6:46am UTC](https://www.ruby-forum.com/t/collection-of-check-boxes-and-labels/146841/2 "2008-08-26T06:46:11Z")

</div>

John H. wrote:

> I want to use a label and I know the “for” property in the label needs  
> to refer to the id of the input tag. However, my id for the input tag  
> is “books[]” for all books.
> 
> This has the benefit of creating a collection in params[:books] but I  
> can’t figure out how to set the label.

Something like this should work, and also allow you to retain  
the checks after an invalid post:

\<%= check\_box\_tag “books[]”, book.id,  
params[:books] && params[:books].include?(book.id.to\_s),  
:id =\> “book\_#{book.id}\_cb”  
%\>

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [August 26, 2008, 7:00am UTC](https://www.ruby-forum.com/t/collection-of-check-boxes-and-labels/146841/3 "2008-08-26T07:00:25Z")

</div>

Thanks, Mark! It worked. Appreciate the help.
