Check_box


#1

I’ve got a question on the use of check_box.


This is working but there just has to be a better way:

RHTML:
<% for book in @books do %>

<%=book.label%> <%checked = book.subscribed ? 'checked': 'nope'%> <%=check_box("book" + book.id.to_s, 'checked', :checked => checked)%> <% end %>

RB:
params[“book” + book.id.to_s][‘checked’]

**
I would much rather have something like this in the RHTML:
<% for book in @books do %>

<%=book.label%> <%=check_box('book', 'subscribed')%> <% end %>

The issue I run into though is that the naming convention ends up
being the same for all of my books (since I am referring to them as
“book” when the check_box is created.

something like: <%=check_box(‘book.label’, ‘subscribed’%> results in
errors as well…

Like I said, the first block if working for me but… let me know of
any alternatives.

Thanks in advance

  • Jim

#2

I would do something like

check_box_tag(‘books[’ + book.id.to_s + ‘][subscribed]’, ‘whatever’,
book.subscribed)

But I’m relatively new so that might not be the best way.

This will results in hash that looks like

params[:books][0][:subscribed] => ‘whatever’

if checked


#3

Jim,

You can pass an :index parameter to the check_box method (and
text_field, and probably a few others) like this:

check_box :mycollection, :available, :index => “1”

which outputs html like this:

So you can easily iterate through this just by getting the indexes you
expect on the other side. Remember that the index will always be a
string in the params hash though…

HTH,
Dan