Forum: Ruby on Rails newbie stumped - when form validates, normally return is fine, but when I select a certain checkbox,

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Rick (Guest)
on 2008-12-09 21:52
(Received via mailing list)
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: http://img.skitch.com/20081209-kcbuias6u29g4ce8a3w...
figure 2: http://img.skitch.com/20081209-1ceh84wc8dnisfjuiim...
figure 3: http://img.skitch.com/20081209-gwk2ib89c2kfm1hhs3y...

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 in case anyway wants to help
by taking a look.

--
Rick
Shandy N. (Guest)
on 2008-12-09 22:22
What is your log saying?
Rick (Guest)
on 2008-12-09 23:01
(Received via mailing list)
On Tue, Dec 9, 2008 at 3:22 PM, Shandy N.
<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 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: http://img.skitch.com/20081209-kcbuias6u29g4ce8a3w...
figure 2: http://img.skitch.com/20081209-1ceh84wc8dnisfjuiim...
figure 3: http://img.skitch.com/20081209-gwk2ib89c2kfm1hhs3y...

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 in case anyway wants to help
by taking a look.
Frederick C. (Guest)
on 2008-12-09 23:25
(Received via mailing list)
On Dec 9, 9:00 pm, Rick <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 http://www.spacevatican.org/2008/7/18/parametrised...
)

Fred
Rick (Guest)
on 2008-12-10 00:12
(Received via mailing list)
On Tue, Dec 9, 2008 at 4:24 PM, Frederick C.
<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 http://www.spacevatican.org/2008/7/18/parametrised...
> )
>


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.
Rick (Guest)
on 2008-12-10 17:56
(Received via mailing list)
On Tue, Dec 9, 2008 at 4:24 PM, Frederick C.
<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

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

<input id="match_new_game_attributes__home"
name="match[new_game_attributes][][home]" type="checkbox" value="1"
/><input name="match[new_game_attributes][][home]" type="hidden"
value="0" /> Yes

<input id="match_new_game_attributes__score"
name="match[new_game_attributes][][score]" size="30" type="text" />

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

just uses built in sql-lite
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.
Frederick C. (Guest)
on 2008-12-10 18:28
(Received via mailing list)
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
>

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
This topic is locked and can not be replied to.