Ok, so I''m using a postgresql db and I''ve got a boolean column, which I''m representing with a check box on my edit/new actions. Now, the check box will properly reflect the value of what is stored in the database: it''s checked if it''s true, and unchecked if it''s false. But no matter what I set the checkbox to, it always comes across as unchecked (false) when being submitted, and I can''t wrap my head around the HTML that this function generates. So this is what I''ve tested: - If the database has a true value, then editing the item will show the checked box. if I uncheck the box and submit, then it properly gets set to false in the db. - if the database has a false value, then editing the item will show the unchecked box. however, checking the box and submitting, it stays false in the db. - also, if it starts out checked and I leave it checked, it turns false in the db anyway. Basically what''s happening is that the checkbox will properly reflect the state of the db, except that submitting the form, no matter what, unchecks the box. And I think I know why... it''s because this is the HTML that gets generated by the check_box function: <input id="article_public" name="article[public]" type="checkbox" value="1" /> <input name="article[public]" type="hidden" value="0" /> It seems to me that the hidden element will set the value to false, no matter what the checkbox is actually set to. This HTML is consistent with what the rails documentation says it will do. My question is... why the hell is rails doing this? What possible purpose does that hidden form element have, except to break the checkbox? -- Urban Artography http://artography.ath.cx
On 4/14/05, Rob Park <rbpark-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Basically what''s happening is that the checkbox will properly reflect > the state of the db, except that submitting the form, no matter what, > unchecks the box.Sorry, this is unclear. What I meant to say is that the checkbox properly reflects the state of the db, but if I submit the form, no matter what, it sets the db value to false. I seem to have worked around this problem by putting this code in the controller: @params[''article''][''public''] == "1" ? @params[''article''][''public''] = true : @params[''article''][''public''] = false It''s really, really ugly, but it seems to fix the check_box''s. Can somebody please enlighten me -- why is this necessary? I think this is a bug in rails... -- Urban Artography http://artography.ath.cx
On 4/15/05, Rob Park <rbpark-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 4/14/05, Rob Park <rbpark-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Basically what''s happening is that the checkbox will properly reflect > > the state of the db, except that submitting the form, no matter what, > > unchecks the box. > > Sorry, this is unclear. What I meant to say is that the checkbox > properly reflects the state of the db, but if I submit the form, no > matter what, it sets the db value to false. > > I seem to have worked around this problem by putting this code in the > controller: > > @params[''article''][''public''] == "1" ? > @params[''article''][''public''] = true : > @params[''article''][''public''] = false > > It''s really, really ugly, but it seems to fix the check_box''s. Can > somebody please enlighten me -- why is this necessary? I think this is > a bug in rails...The hidden field is explained in the rdoc, quoting: "Usually unchecked checkboxes don''t post anything. We work around this problem by adding a hidden value with the same name as the checkbox." It''s not the cause of your problem anyway as @params[''article''][''public''] is set to "1" (or your controller change wouldn''t do anything. What''s the exact datatype of the column in the database? could you paste the DDL please? If you''re unable to resolve this please go ahead and file a ticket at http://dev.rubyonrails.org/> -- > Urban Artography > http://artography.ath.cx > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers Koz
Rob, My guess would be that it''s in the Postgresql adapter that this issue is popping up. I''m building an adapter for Firebird and have run into some similar issues. When the field is passed to the adapter for formatting - the code looks at the Ruby type of the field and formats it in that manner. In this case instead of the adapter seeing a boolean it sees a fixnum and formats it that way. Postgresql probably takes anything other then TRUE as false and therefore the field gets updated incorrectly. That''s why it works when you use the if statement because now the ruby field is of type boolean and the adapter formats it correctly for you. I would create a ticket and see if the maintainer of the adapter can get the bug straightened out (basically it requires looking not only to the ruby field type but to the database field type as well). John W Higgins develop-U23jnKMpDSxBDgjK7y7TUQ@public.gmane.org Quoting Rob Park <rbpark-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> On 4/14/05, Rob Park <rbpark-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Basically what''s happening is that the checkbox will properly reflect > > the state of the db, except that submitting the form, no matter what, > > unchecks the box. > > Sorry, this is unclear. What I meant to say is that the checkbox > properly reflects the state of the db, but if I submit the form, no > matter what, it sets the db value to false. > > I seem to have worked around this problem by putting this code in the > controller: > > @params[''article''][''public''] == "1" ? > @params[''article''][''public''] = true : > @params[''article''][''public''] = false > > It''s really, really ugly, but it seems to fix the check_box''s. Can > somebody please enlighten me -- why is this necessary? I think this is > a bug in rails... > > -- > Urban Artography > http://artography.ath.cx > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program.
I had the same problem using dropdown boxes ("true"/"false") and the boolean type in postgresql. I eventually just made my boolean fields varchar(5) default ''true'' and used: As far as I remember I think it was something to do with postgresql returning logical false and ruby trying to use the string "false". Something along those lines. <%= select("region", "active", {"True" => "true", "False" => "false"}) %> Michael>>>Basically what''s happening is that the checkbox will properly reflect >>>the state of the db, except that submitting the form, no matter what, >>>unchecks the box. >> >>Sorry, this is unclear. What I meant to say is that the checkbox >>properly reflects the state of the db, but if I submit the form, no >>matter what, it sets the db value to false. >> >>I seem to have worked around this problem by putting this code in the >>controller: >> >> @params[''article''][''public''] == "1" ? >> @params[''article''][''public''] = true : >> @params[''article''][''public''] = false >> >>It''s really, really ugly, but it seems to fix the check_box''s. Can >>somebody please enlighten me -- why is this necessary? I think this is >>a bug in rails... > > > The hidden field is explained in the rdoc, quoting: > > "Usually unchecked checkboxes don''t post anything. We work around this > problem by adding a hidden value with the same name as the checkbox." > > It''s not the cause of your problem anyway as > @params[''article''][''public''] is set to "1" (or your controller change > wouldn''t do anything. > > What''s the exact datatype of the column in the database? could you > paste the DDL please? > > If you''re unable to resolve this please go ahead and file a ticket at > http://dev.rubyonrails.org/
On 4/16/05, John W Higgins <develop-U23jnKMpDSxBDgjK7y7TUQ@public.gmane.org> wrote:> Rob, > > My guess would be that it''s in the Postgresql adapter that this issue is popping > up. I''m building an adapter for Firebird and have run into some similar issues. > When the field is passed to the adapter for formatting - the code looks at the > Ruby type of the field and formats it in that manner. In this case instead of > the adapter seeing a boolean it sees a fixnum and formats it that way. > Postgresql probably takes anything other then TRUE as false and therefore the > field gets updated incorrectly. That''s why it works when you use the if > statement because now the ruby field is of type boolean and the adapter formats > it correctly for you. > > I would create a ticket and see if the maintainer of the adapter can get the bug > straightened out (basically it requires looking not only to the ruby field type > but to the database field type as well).I see that a ticket was created for this. http://dev.rubyonrails.org/ticket/1114. It looks like it was resolved in svn already. Can someone with postgres please test the fix (http://dev.rubyonrails.org/ticket/995) and if it works, close ticket 1114?> John W Higgins > develop-U23jnKMpDSxBDgjK7y7TUQ@public.gmane.org > > > Quoting Rob Park <rbpark-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > On 4/14/05, Rob Park <rbpark-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Basically what''s happening is that the checkbox will properly reflect > > > the state of the db, except that submitting the form, no matter what, > > > unchecks the box. > > > > Sorry, this is unclear. What I meant to say is that the checkbox > > properly reflects the state of the db, but if I submit the form, no > > matter what, it sets the db value to false. > > > > I seem to have worked around this problem by putting this code in the > > controller: > > > > @params[''article''][''public''] == "1" ? > > @params[''article''][''public''] = true : > > @params[''article''][''public''] = false > > > > It''s really, really ugly, but it seems to fix the check_box''s. Can > > somebody please enlighten me -- why is this necessary? I think this is > > a bug in rails... > > > > -- > > Urban Artography > > http://artography.ath.cx > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > ---------------------------------------------------------------- > This message was sent using IMP, the Internet Messaging Program. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers Koz