Re: How to neatly handle multiple checkboxes with Rails

Michael Campbell
Thu, 09 Jun 2005 04:31:51 -0000

On 6/8/05, Michael Campbell
<michael.campbell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:

> If I iterate over a check_box tag (using a render_partial_collection),
> what I get is something like:
>
> <input type="checkbox"  name="object[method]"
value="2"> foo
> <input type="checkbox"  name="object[method]"
value="3"> bar
> <input type="checkbox"  name="object[method]"
value="4"> baz
> <input type="checkbox"  name="object[method]"
value="10"> blurfle
> ...
>
> but if I select multiple, params has only 1, due to the same key in each
one:
>
> @params => {object => {method => 2}}  (or 3, or 4, or 10, but
never
> more than one of those)
>
> How do I structure the checkbox tag (or can I?) so that I can end up
> with some indication that (ex.) both "foo" and "bar"
were checked?


Ok, after some help on IRC, I think I cannot use the check_box helper.
 Which is OK.  Something like this seems to work:

<input type="checkbox" name="something[]"
value="<%=something.id%>">
<%= something.name %>

If I have multiple of those, and I select more than one, I get:

@params => something=>[2, 10]

Voila!

Can someone point out where this having an empty brace pair in the
name exhibits this "put multiples into an array" behavior?  I
haven''t
run across it that I can recall.

Thanks!