Rick
2008-Dec-10 17:47 UTC
[SOLVED thanks Frederick] was Re: newbie stumped - when form validates, normally return is fine, but when I select a certain check
Thanks Frederick. Your explanation was perfect! (Now I''ll work on figuring out how to handle the problem creating the checkbox manually, but you got me on the right path.) (Seriously, send my a paypal link or else I''ll be getting you a gift certificate to amazon or something:) On Wed, Dec 10, 2008 at 11:27 AM, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On 10 Dec 2008, at 15:55, Rick wrote: > >> >> On Tue, Dec 9, 2008 at 4:24 PM, Frederick Cheung >> <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 >> > > 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 > > >> 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. >> >> > > > > > >-- Rick --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Dec-10 18:14 UTC
Re: [SOLVED thanks Frederick] was Re: newbie stumped - when form validates, normally return is fine, but when I select a certain check
On 10 Dec 2008, at 17:47, Rick wrote:> > Thanks Frederick. Your explanation was perfect! (Now I''ll work on > figuring out how to handle the problem creating the checkbox manually, > but you got me on the right path.) > > (Seriously, send my a paypal link or else I''ll be getting you a gift > certificate to amazon or something:)Don''t mention it, all in a day''s play :-)> > > On Wed, Dec 10, 2008 at 11:27 AM, Frederick Cheung > <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> >> On 10 Dec 2008, at 15:55, Rick wrote: >> >>> >>> On Tue, Dec 9, 2008 at 4:24 PM, Frederick Cheung >>> <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 >>> >> >> 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 >> >> >>> 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. >>> >>>> >> >> >>> >> > > > > -- > Rick > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---