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???
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.
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???
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.
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
)
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;
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
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.
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
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'}]}}
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
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