# Newbie stumped - when form validates, normally return is fine, but when I select a certain checkbox,

**URL:** <https://www.ruby-forum.com/t/newbie-stumped-when-form-validates-normally-return-is-fine-but-when-i-select-a-certain-checkbox/155246>\
**Category:** Rails\
**Created:** [December 9, 2008, 8:52pm UTC](https://www.ruby-forum.com/t/newbie-stumped-when-form-validates-normally-return-is-fine-but-when-i-select-a-certain-checkbox/155246 "2008-12-09T20:52:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [December 9, 2008, 8:52pm UTC](https://www.ruby-forum.com/t/newbie-stumped-when-form-validates-normally-return-is-fine-but-when-i-select-a-certain-checkbox/155246/1 "2008-12-09T20:52:42Z")

</div>

I’m really stumped here. I’m new to Rails and I’m sure I’m doing  
something stupid but what’s going on seems like voodoo or something.  
When you click on “new match” the controller makes a call to set up  
two new objects to be populated. You’re brought to your form (see  
figure 1). When you submit a form with improper validation, typically  
things work fine - validation errors show up and you are returned to  
the form with the two objects populated with what you had, and the  
errors display (see figure 2.)

The part that has me so stumped is that when you happen to check the  
“home” checkbox and you submit, I end up with an extra object created  
when validation fails! (see figure 3, you’ll see the extra player game  
object column.) I have no idea why, and I have no idea why selecting  
this checkbox causes it to happen? Why does only this form field cause  
the problem and not the other form fields???

figure 1: [Skitch | Evernote](http://img.skitch.com/20081209-kcbuias6u29g4ce8a3wuyu6xcf.jpg)  
figure 2: [Skitch | Evernote](http://img.skitch.com/20081209-1ceh84wc8dnisfjuiimw7fk9qe.jpg)  
figure 3: [Skitch | Evernote](http://img.skitch.com/20081209-gwk2ib89c2kfm1hhs3ymh7fc4e.jpg)

Can someone give me some tips as to what even might be wrong or where  
I should start to look? I have some log statements and the same flow  
seems to happen regardless of the scenario.

I posted the main controller and a few other things that I think might  
be relevant here [http://pastie.org/335194](http://pastie.org/335194) in case anyway wants to help  
by taking a look.

–  
Rick

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [December 9, 2008, 9:22pm UTC](https://www.ruby-forum.com/t/newbie-stumped-when-form-validates-normally-return-is-fine-but-when-i-select-a-certain-checkbox/155246/2 "2008-12-09T21:22:24Z")

</div>

What is your log saying?

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [December 9, 2008, 10:25pm UTC](https://www.ruby-forum.com/t/newbie-stumped-when-form-validates-normally-return-is-fine-but-when-i-select-a-certain-checkbox/155246/3 "2008-12-09T22:25:07Z")

</div>

On Dec 9, 9:00 pm, Rick [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Tue, Dec 9, 2008 at 3:22 PM, Shandy N.
> 
> Can someone give me some tips as to what even might be wrong or where  
> I should start to look? I have some log statements and the same flow  
> seems to happen regardless of the scenario.

Is this rails check box trickery tripping you up ? The rails check box  
helper creates a hidden input field with the same name (and value 0)  
as well as the actual check box. If you’re using arrays of parameters  
like I think you are this can mess things up. If so then one work  
around is to use check\_box\_tag instead of check\_box (but which has the  
downside of when the box is not ticked you won’t get any param for it  
rather than receiving a ‘0’. The other way out is to make your params  
a hash keyed by indices rather than an array (I mention this at the  
bottom of [Parametrised to the max - Space Vatican](http://www.spacevatican.org/2008/7/18/parametrised-to-the-max)  
)

Fred

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [December 9, 2008, 11:12pm UTC](https://www.ruby-forum.com/t/newbie-stumped-when-form-validates-normally-return-is-fine-but-when-i-select-a-certain-checkbox/155246/4 "2008-12-09T23:12:42Z")

</div>

On Tue, Dec 9, 2008 at 4:24 PM, Frederick C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> helper creates a hidden input field with the same name (and value 0)  
> as well as the actual check box. If you’re using arrays of parameters  
> like I think you are this can mess things up. If so then one work  
> around is to use check\_box\_tag instead of check\_box (but which has the  
> downside of when the box is not ticked you won’t get any param for it  
> rather than receiving a ‘0’. The other way out is to make your params  
> a hash keyed by indices rather than an array (I mention this at the  
> bottom of [Parametrised to the max - Space Vatican](http://www.spacevatican.org/2008/7/18/parametrised-to-the-max)  
> )

Thanks Fred, I will look into this tonight and try some of these  
suggestions out.

How it ends up interfering to the point that a “new nested object” is  
created other than the two I originally started with though, seems  
really weird.

I look forward to seeing if your suggestions fix things.

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [December 9, 2008, 10:01pm UTC](https://www.ruby-forum.com/t/newbie-stumped-when-form-validates-normally-return-is-fine-but-when-i-select-a-certain-checkbox/155246/5 "2008-12-09T22:01:34Z")

</div>

On Tue, Dec 9, 2008 at 3:22 PM, Shandy N.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What is your log saying?

The log doesn’t show much that seems to help me. Here is the log  
[http://pastie.org/335259](http://pastie.org/335259) after the “create” button is hit and the form  
submits. Notice at the end you’ll see “3” calls to something like:

Compiling template  
\_run\_erb\_47app47views47matches47\_player\_game46html46erb\_locals\_g\_object\_player\_game  
[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT \* FROM “players” ORDER BY  
first\_name ASC[0m  
[4;35;1mCACHE (0.0ms)[0m [0mSELECT \* FROM “teams” ORDER BY name  
ASC[0m

When I think you should only see “2.” (and actually you will only see  
‘2’ if you don’t select the ‘home’ checkbox.) Any time you submit the  
form and the checkbox (“home”) is selected this happens. If I don’t  
select ‘just’ that checkbox things work fine.

[ORIGINAL POST:]

I’m really stumped here. I’m new to Rails and I’m sure I’m doing  
something stupid but what’s going on seems like voodoo or something.  
When you click on “new match” the controller makes a call to set up  
two new objects to be populated. You’re brought to your form (see  
figure 1). When you submit a form with improper validation, typically  
things work fine - validation errors show up and you are returned to  
the form with the two objects populated with what you had, and the  
errors display (see figure 2.)

The part that has me so stumped is that when you happen to check the  
“home” checkbox and you submit, I end up with an extra object created  
when validation fails! (see figure 3, you’ll see the extra player game  
object column.) I have no idea why, and I have no idea why selecting  
this checkbox causes it to happen? Why does only this form field cause  
the problem and not the other form fields???

figure 1: [Skitch | Evernote](http://img.skitch.com/20081209-kcbuias6u29g4ce8a3wuyu6xcf.jpg)  
figure 2: [Skitch | Evernote](http://img.skitch.com/20081209-1ceh84wc8dnisfjuiimw7fk9qe.jpg)  
figure 3: [Skitch | Evernote](http://img.skitch.com/20081209-gwk2ib89c2kfm1hhs3ymh7fc4e.jpg)

Can someone give me some tips as to what even might be wrong or where  
I should start to look? I have some log statements and the same flow  
seems to happen regardless of the scenario.

I posted the main controller and a few other things that I think might  
be relevant here [http://pastie.org/335194](http://pastie.org/335194) in case anyway wants to help  
by taking a look.

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [December 10, 2008, 5:28pm UTC](https://www.ruby-forum.com/t/newbie-stumped-when-form-validates-normally-return-is-fine-but-when-i-select-a-certain-checkbox/155246/6 "2008-12-10T17:28:42Z")

</div>

On 10 Dec 2008, at 15:55, Rick wrote:

> In what ways does it mess things up? (I know things get screwy as I  
> posted in my original post, yet have no idea WHY the before I’m seeing  
> is happening, where a new object ends up being created.) I posted some  
> of the raw html here [http://pastie.org/335814](http://pastie.org/335814)

It all comes down to how array parameters are handled. If the  
parameters you get look like  
match[new\_game\_attributes][][foo]=bar  
match[new\_game\_attributes][][bar]=baz

then you get params == {:match =\> {:new\_game\_attributes =\> [{:foo =\>  
‘bar’, :bar =\> ‘baz’}]}}

if you had

match[new\_game\_attributes][][foo]=bar  
match[new\_game\_attributes][][bar]=baz

match[new\_game\_attributes][][foo]=alice  
match[new\_game\_attributes][][bar]=bob

then you’d expect params == {:match =\> {:new\_game\_attributes =\> [{:foo  
=\> ‘bar’, :bar =\> ‘baz’}, {:foo =\> ‘alice’, :bar =\> 'bob}]}}

The key thing here to note is that it is the repetition of a parameter  
that causes rails to add a new array element. so if your checkbox is  
checked and your parameters look like

match[new\_game\_attributes][][name]=fred  
match[new\_game\_attributes][][home]=0  
match[new\_game\_attributes][][home]=1

then you’ll get parameters like {:match =\> {:new\_game\_attributes =\>  
[{:name =\> ‘fred’, :home =\> ‘0’}, {:home =\> ‘1’}]}}  
ie two array elements where you only wanted one, which (as I  
understood it) was the problem you were grappling with

Fred

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [December 10, 2008, 4:56pm UTC](https://www.ruby-forum.com/t/newbie-stumped-when-form-validates-normally-return-is-fine-but-when-i-select-a-certain-checkbox/155246/7 "2008-12-10T16:56:54Z")

</div>

On Tue, Dec 9, 2008 at 4:24 PM, Frederick C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is this rails check box trickery tripping you up ? The rails check box  
> helper creates a hidden input field with the same name (and value 0)  
> as well as the actual check box. If you’re using arrays of parameters  
> like I think you are this can mess things up.

In what ways does it mess things up? (I know things get screwy as I  
posted in my original post, yet have no idea WHY the before I’m seeing  
is happening, where a new object ends up being created.) I posted some  
of the raw html here [http://pastie.org/335814](http://pastie.org/335814)

but as a summary, yes the checbox field is part of an array and looks  
like;

Yes

Even when validation fails the form returns correctly with the  
checkbox checked that I selected and everything looks fine, except an  
entire stinkin’ new match[new\_game\_attributes] object was created!!  
It’s driving me nuts. Why would a checkbox cause this behavior?

I’ll pay someone 15 bucks (I’d pay more, but hey, the economy:) if  
they can help me figure it out just so I can learn. I put the little  
app here

[http://dl-client.getdropbox.com/u/86998/hockey.zip](http://dl-client.getdropbox.com/u/86998/hockey.zip)

just uses built in sql-lite  
[http://localhost:3000/matches](http://localhost:3000/matches) select new match, (uesrname: admin  
password: wombat) and then check one of the ‘home’ checkboxes and  
notice you’ll end up with an extra instance of a player game on the  
form when validation fails. If you DO NOT check that checkbox the  
extra instance is not created when validation fails.
