Collection of Check Boxes and Labels

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

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.

Thanks, Mark! It worked. Appreciate the help.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs