check_box not passing 0 when unchecked?

948 views
Skip to first unread message

Joan Gu

unread,
Sep 11, 2011, 11:37:00 PM9/11/11
to rubyonra...@googlegroups.com
Code:
<%= check_box("schedule_report", "active_#{id}",
options={:id=>"schedule_report_#{id}_active", :checked
=>schedule_report.active }, 1, 0) %>

Rendered output:
<input type="hidden" value="0" name="schedule_report[active_20]">
<input id="schedule_report_20_active" type="checkbox" value="1"
name="schedule_report[active_20]" checked="checked">

<input type="hidden" value="0" name="schedule_report[active_17]">
<input id="schedule_report_17_active" type="checkbox" value="1"
name="schedule_report[active_17]">
.......

Problem:
The value of "1" is always passed, even if the box is UNCHECKED.
Please help!

Thanks!

--
Posted via http://www.ruby-forum.com/.

7stud --

unread,
Sep 12, 2011, 2:10:36 AM9/12/11
to rubyonra...@googlegroups.com
Go read a basic html tutorial, concentrating on the syntax of an <input>
tag.

Dave Aronson

unread,
Sep 12, 2011, 7:02:00 AM9/12/11
to rubyonra...@googlegroups.com

Passed to your browser as evidenced by the above renderings (which
would be correct), or as in, the form always returns a 1? Big
difference. In the latter case, we'd need to know what browser (and
possibly OS), unless you mean all. Could be just a browser bug.

-Dave

--
Main Web Site: davearonson.com | LOOKING FOR WORK,
Programming Blog: codosaur.us  | preferably RoR, in NoVa/DC;
Excellence Blog:  dare2xl.com  | see main web site.

7stud --

unread,
Sep 12, 2011, 9:59:12 AM9/12/11
to rubyonra...@googlegroups.com
Dave Aronson wrote in post #1021425:

> On Sun, Sep 11, 2011 at 23:37, Joan Gu <li...@ruby-forum.com> wrote:
>
>> <input type="hidden" value="0" name="schedule_report[active_17]">
>> <input id="schedule_report_17_active" type="checkbox" value="1"
>> name="schedule_report[active_17]">
>> .......
>>
>> Problem:
>> The value of "1" is always passed, even if the box is UNCHECKED.
>> Please help!
>
> Passed to your browser as evidenced by the above renderings (which
> would be correct)

I also suggest that you go read a beginning html tutorial.

Dave Aronson

unread,
Sep 12, 2011, 10:13:14 AM9/12/11
to rubyonra...@googlegroups.com
On Mon, Sep 12, 2011 at 09:59, 7stud -- <li...@ruby-forum.com> wrote:

> I also suggest that you go read a beginning html tutorial.

Howzabout, rather than just getting all snarky, you point out to Joan
and me, what's going wrong? MINSWAN, remember?

Frederick Cheung

unread,
Sep 12, 2011, 10:45:15 AM9/12/11
to Ruby on Rails: Talk
So what does the code calling this look like (i.e. what collection are
you iterating over) ? What do the received params look like ?

Fred

Michael Pavling

unread,
Sep 12, 2011, 3:11:21 PM9/12/11
to rubyonra...@googlegroups.com
On 12 September 2011 15:13, Dave Aronson

<googlegr...@davearonson.com> wrote:
> On Mon, Sep 12, 2011 at 09:59, 7stud -- <li...@ruby-forum.com> wrote:
>
>> I also suggest that you go read a beginning html tutorial.
>
> Howzabout, rather than just getting all snarky, you point out to Joan
> and me, what's going wrong?  MINSWAN, remember?

+1

I've been wracking my brains to see what's wrong with the HTML
(especially since it's been generated with the check_box() helper).
Maybe I'm missing something obvious... ?

Chris Mear

unread,
Sep 13, 2011, 10:04:13 AM9/13/11
to rubyonra...@googlegroups.com
On 12 Sep 2011, at 20:11, Michael Pavling wrote:

> I've been wracking my brains to see what's wrong with the HTML
> (especially since it's been generated with the check_box() helper).
> Maybe I'm missing something obvious... ?

I don't think you're missing anything -- I've tested Joan's code in Safari and it works as expected (you find '0' in the params when you uncheck a box).

I do seem to remember some weirdness with Rails' checkboxes/hidden fields pairs and Firefox, to do with the way it restores form state when you go back to (or even reload) a form. However, I couldn't replicate the problem in Firefox 6.0.1, so maybe it's an old bug or maybe I'm misremembering things.

Joan, are you testing in Firefox by any chance?

Chris

Joan Gu

unread,
Sep 13, 2011, 10:39:50 AM9/13/11
to rubyonra...@googlegroups.com
Thank you all for the help.

Yes, the test was done in firefox 6.0.2 and the params were passed
through an ajax link by checking checkbox value (jquery). Can't paste
the code here coz I can't access the source now.

Frederick Cheung

unread,
Sep 13, 2011, 12:37:57 PM9/13/11
to Ruby on Rails: Talk


On Sep 13, 3:39 pm, Joan Gu <li...@ruby-forum.com> wrote:
> Thank you all for the help.
>
> Yes, the test was done in firefox 6.0.2 and the params were passed
> through an ajax link by checking checkbox value (jquery). Can't paste
> the code here coz I can't access the source now.
>
Ah, ajax may be the key. How are you serialising the form inputs? If
you're always submitting the checkbox's value then it will indeed only
ever submit '1'. You'd need to select the appropriate value based on
whether the checkbox was checked.

Fred

Joan Gu

unread,
Sep 14, 2011, 12:32:14 AM9/14/11
to rubyonra...@googlegroups.com
Frederick Cheung wrote in post #1021765:

> Ah, ajax may be the key. How are you serialising the form inputs? If
> you're always submitting the checkbox's value then it will indeed only
> ever submit '1'. You'd need to select the appropriate value based on
> whether the checkbox was checked.
>
> Fred

Hi Fred,

Yes, you are right, the ajax code passing checkbox's value is the root
cause.

Problem fixed and thanks for help.

Joan

Reply all
Reply to author
Forward
0 new messages