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: <input id="books[]" name="books[]" type="checkbox" value="306" /> 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
John Honovich wrote:> 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: <input id="books[]" name="books[]" > type="checkbox" value="306" /> 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.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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks, Mark! It worked. Appreciate the help. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---