# List of unchecked checkboxes

**URL:** <https://www.ruby-forum.com/t/list-of-unchecked-checkboxes/126385>\
**Category:** Rails\
**Created:** [January 17, 2008, 10:10pm UTC](https://www.ruby-forum.com/t/list-of-unchecked-checkboxes/126385 "2008-01-17T22:10:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![enduro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@enduro](https://www.ruby-forum.com/u/enduro)\
**Post date:** [January 17, 2008, 10:10pm UTC](https://www.ruby-forum.com/t/list-of-unchecked-checkboxes/126385/1 "2008-01-17T22:10:30Z")

</div>

Hi all

Having kinda trouble with checkboxes. Let’s assume “Order  
has\_and\_belongs\_to\_many :products” and vice versa. Now let’s assume  
there are products with IDs 1, 2 and 3 in the db and I’m editing an  
existing Order with the following view:

\<% form\_for(:order) do |f| %\>  
\<% f.check\_box(:product\_ids, 1) %\>  
\<% f.check\_box(:product\_ids, 2) %\>  
\<% f.check\_box(:product\_ids, 3) %\>  
\<% end %\>

First let’s set all checkboxes. The resulting params hash will be:  
param[:order][:product\_ids] # =\> [‘1’, ‘2’, ‘3’]

This is perfect as the controller code will do as desired:  
@order.update\_attributes(params[:order])  
@order.product\_ids # =\> [1,2,3]

But now the same with all checkboxes NOT set. The params hash:  
param[:order][:product\_ids] # =\> nil

Way uncool, because this is what the controller does:  
@order.update\_attributes(params[:order])  
@order.product\_ids # =\> [1,2,3]

It _would_ be cool though to have an empty array instead of nil in the  
params hash there as this would correctly remove all referenced products  
from this order.

Now I could of course add this to the controller:  
@order.product\_ids = [] if @order.product\_ids.nil?

But as my view code is generated by a FormHelper plugin, I’d like to  
make it work without the need to touch the controller code.

Can I add anything to the view that will ensure the params hash will  
return [] rather than nil if no checkbox is set?

Thanks for your help, -sven

---

<div class="post-metadata">

**Author:** ![enduro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@enduro](https://www.ruby-forum.com/u/enduro)\
**Post date:** [January 17, 2008, 10:18pm UTC](https://www.ruby-forum.com/t/list-of-unchecked-checkboxes/126385/2 "2008-01-17T22:18:40Z")

</div>

You could do this before calling update\_attributes:

param[:order][:product\_ids] ||= []

This will initialize the product\_ids parameter to an empty array if it  
is nil.

If you want to make this the default behavior, you’ll need to open up  
some Rails code. Not sure exactly where you’d make a change, but it  
should be possible in just a few lines of code.

---

<div class="post-metadata">

**Author:** ![enduro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@enduro](https://www.ruby-forum.com/u/enduro)\
**Post date:** [January 18, 2008, 10:00am UTC](https://www.ruby-forum.com/t/list-of-unchecked-checkboxes/126385/3 "2008-01-18T10:00:19Z")

</div>

> \<% form\_for(:order) do |f| %\>  
> \<% f.check\_box(:product\_ids, 1) %\>  
> \<% f.check\_box(:product\_ids, 2) %\>  
> \<% f.check\_box(:product\_ids, 3) %\>  
> \<% end %\>

This is wrong anyway, should be:

\<% form\_for(:order) do |f| %\>  
\<% f.check\_box(‘product\_ids[]’, 1) %\>  
\<% f.check\_box(‘product\_ids[]’, 2) %\>  
\<% f.check\_box(‘product\_ids[]’, 3) %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![enduro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@enduro](https://www.ruby-forum.com/u/enduro)\
**Post date:** [January 18, 2008, 10:53am UTC](https://www.ruby-forum.com/t/list-of-unchecked-checkboxes/126385/4 "2008-01-18T10:53:18Z")

</div>

I found a solution myself 🙂 Seems to work at least with the UrlParser  
in Rails 2.0.2.

\<% form\_for(:order) do |f| %\>  
\<% f.hidden\_input(‘product\_ids[]’, ‘’) %\>  
\<% f.check\_box(‘product\_ids[]’, 1) %\>  
\<% f.check\_box(‘product\_ids[]’, 2) %\>  
\<% f.check\_box(‘product\_ids[]’, 3) %\>  
\<% end %\>

The empty value is skipped when the IDs are compiled, but it enforces an  
[] in the params hash if nonoe of the checkboxes are selected.

---

<div class="post-metadata">

**Author:** ![enduro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@enduro](https://www.ruby-forum.com/u/enduro)\
**Post date:** [January 25, 2008, 7:00am UTC](https://www.ruby-forum.com/t/list-of-unchecked-checkboxes/126385/5 "2008-01-25T07:00:23Z")

</div>

I have a similar problem, but but your solution doesn’t seem to work  
for me.

I have something like this:  
\<% form\_for…

\<%for category in @categories do%\>  
\<% f.check\_box(‘category\_ids[]’,category.id)%\>  
\<%end%\>

\<%end%\>

But I get a  
“undefined method `merge’ for 1:Fixnum”

on the check\_box line.

Any ideas on whats different? If I hard code the id as you have I get  
the same result.

Thanks,  
j

On Jan 18, 1:53 am, Sven S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![enduro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@enduro](https://www.ruby-forum.com/u/enduro)\
**Post date:** [January 25, 2008, 11:46pm UTC](https://www.ruby-forum.com/t/list-of-unchecked-checkboxes/126385/6 "2008-01-25T23:46:30Z")

</div>

Thanks, but I can’t seem to make that version of the fuction work  
either. That’s ok though I’ll look elswhere to find a working example  
of what I’m trying to do, just thought this was it.

Thanks,  
j

On Jan 25, 1:55 am, Sven S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![enduro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@enduro](https://www.ruby-forum.com/u/enduro)\
**Post date:** [January 25, 2008, 10:55am UTC](https://www.ruby-forum.com/t/list-of-unchecked-checkboxes/126385/7 "2008-01-25T10:55:40Z")

</div>

> \<%for category in @categories do%\>  
> \<% f.check\_box(‘category\_ids[]’,category.id)%\>  
> \<%end%\>

I’m using check\_box\_tag (for contextual reasons, it’s in a plugin), but  
it looks to me as if you use the check\_box helper incorrectly.

According to the doc at [http://www.railsapi.org/check\_box](http://www.railsapi.org/check_box) it should be  
as follows:

f.check\_box(method, options = {}, checked\_value = “1”, unchecked\_value =  
“0”)

So you pass a number (category.id) where a hash (options) is expected.
