# Check\_box submits both "1" and "0" when checked

**URL:** <https://www.ruby-forum.com/t/check-box-submits-both-1-and-0-when-checked/132958>\
**Category:** Rails\
**Created:** [March 26, 2008, 4:03am UTC](https://www.ruby-forum.com/t/check-box-submits-both-1-and-0-when-checked/132958 "2008-03-26T04:03:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brentsoderberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8edcca/32.png) [@brentsoderberg](https://www.ruby-forum.com/u/brentsoderberg)\
**Post date:** [March 26, 2008, 4:03am UTC](https://www.ruby-forum.com/t/check-box-submits-both-1-and-0-when-checked/132958/1 "2008-03-26T04:03:32Z")

</div>

Hi, I have a “Customer” model and a “Product” model, where a Customer  
“has many” Products. When editing a Customer, I also want to display the  
Products that person has at the bottom of the page, available for  
editing, (“complex forms” as described in Railscast 73, 74, and 75).

at the end of Customer’s edit.html.erb:  
…  
\<%= render :partial =\> ‘product’, :collection =\> @customer.products,  
:locals =\> { :f =\> f } %\>

in \_product.html.erb:  
\<% fields\_for “customer[product\_attributes][]”, product do  
|product\_form| %\>

Serial Number  
\<%= product\_form.text\_field :serial\_number, :index =\> nil  
%\>  
 …  
Payment\_received  
\<%= product\_form.check\_box :payment\_received, :index =\> nil  
%\>  
 …

Overall, this scheme is working fine, and I can edit any of the Products  
from the Customer page and update them with no problem. The only  
problem comes when I try to edit the “Payment Received” check box…it  
appears to be submitting “1” and “0” when I check the box and update it.  
As a result, a Customer who originally had 3 Products will, after  
checking the “Payment Received” check box in some of them, have 5 (or 6  
or 7) Products afterwards.

The (messed up) params sent to the Update after checking the boxes are  
as follows:  
(rdb:27) params[:customer][:product\_attributes][0]  
{“remarks”=\>“remark1.”, “product\_id”=\>“72”, “company\_id”=\>“1”,  
“subscription”=\>"  
1", “serial\_number”=\>“4444”}  
(rdb:27) params[:customer][:product\_attributes][1]  
{“payment\_received”=\>“1”, “subscription”=\>“0”}  
(rdb:27) params[:customer][:product\_attributes][2]  
{“remarks”=\>"", “product\_id”=\>“73”, “payment\_received”=\>“0”, “id”=\>“22”,  
“company\_id”=\>“3”,  
“subscription”=\>“1”, “broadcast\_period(1i)”=\>“2008”,  
“broadcast\_period(2i)”=\>“3”  
, “serial\_number”=\>“55555”, “broadcast\_period(3i)”=\>“31”}  
(rdb:27) params[:customer][:product\_attributes][3]  
{“payment\_received”=\>“1”, “subscription”=\>“0”}  
(rdb:27) params[:customer][:product\_attributes][4]  
{“remarks”=\>“remark3.”, “product\_id”=\>“73”, “payment\_received”=\>“0”,  
“id”=\>“23”, "company\_id  
"=\>“4”, “subscription”=\>“1”, “broadcast\_period(1i)”=\>“2008”,  
“broadcast\_period(2  
i)”=\>“3”, “serial\_number”=\>“3333”, “broadcast\_period(3i)”=\>“31”}  
(rdb:27) params[:customer][:product\_attributes][5]  
{“payment\_received”=\>“1”, “subscription”=\>“0”}  
(rdb:27) params[:customer][:product\_attributes][6]  
{“payment\_received”=\>“0”, “id”=\>“24”,  
“broadcast\_period(1i)”=\>“2008”,“broadcast\_period(2i)”  
=\>“3”, “broadcast\_period(3i)”=\>“31”}

Sorry its a little hard to read, but there should only be 3 Products  
with this Customer, but there is now 7. The “payment\_received” (and  
“subscription”) params were checked, and should be “1” but they both  
alternate between “1” and “0”, creating extra Products.

Any idea what is causing this? Everything else (text\_field, text\_area,  
select, date\_select) all work fine, and my form check\_boxes work fine  
elsewhere. The Generated HTML appears to be fine for my check boxes,  
looks the same as the others… Is it because I’m updating a model  
within a model?

Any insight would be greatly appreciated, thank you.

---

<div class="post-metadata">

**Author:** ![brentsoderberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8edcca/32.png) [@brentsoderberg](https://www.ruby-forum.com/u/brentsoderberg)\
**Post date:** [March 26, 2008, 8:39am UTC](https://www.ruby-forum.com/t/check-box-submits-both-1-and-0-when-checked/132958/2 "2008-03-26T08:39:09Z")

</div>

On 26 Mar 2008, at 03:03, Brent S. wrote:

> looks the same as the others… Is it because I’m updating a model  
> within a model?

I’m not entirely sure what is going on, but this bit of information  
might be helpful. By default if you have just a checkbox and it’s not  
checked then the browser submits absolutely nothing. Things would be  
somewhat simpler if it sent false and true in the other case (or 0 and  
1 or whatever).  
To get around this when you use check\_box (but not check\_box\_tag)  
rails creates a hidden input in addition to the checkbox, the input  
input is wired up to send 0 and the checkbox will send nothing or 1.  
It’s also true that parameter names are very significant to rails: if  
you have 2 inputs called bob, then the first one wins (this is what  
should happen with the checkboxes). However if you have 2 inputs  
called bob[] then you’ll get an array called bob in your params.  
I suspect something to do with these 2 things is happening.

Fred

---

<div class="post-metadata">

**Author:** ![brentsoderberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8edcca/32.png) [@brentsoderberg](https://www.ruby-forum.com/u/brentsoderberg)\
**Post date:** [March 26, 2008, 10:42am UTC](https://www.ruby-forum.com/t/check-box-submits-both-1-and-0-when-checked/132958/3 "2008-03-26T10:42:55Z")

</div>

Thanks a lot, that looks like it’s probably the case in this  
situation… I’ll check it out.

Brent
