check_box_tag not passing 0 when unchecked?

240 views
Skip to first unread message

Taylor Strait

unread,
Mar 30, 2007, 3:24:37 AM3/30/07
to rubyonra...@googlegroups.com
Code:
<%= check_box_tag("person[#{person.id}][is_approved]", 1, true) %>

Rendered output:
<input checked="checked" id="person[7][is_approved]"
name="person[7][is_approved]" value="1" type="checkbox">

Problem:
The value of "1" is always passed, even if the box is UNCHECKED.
Suggestions? Thanks!

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

Michael Wang

unread,
Mar 30, 2007, 4:57:46 AM3/30/07
to rubyonra...@googlegroups.com
Taylor Strait wrote:
> Code:
> <%= check_box_tag("person[#{person.id}][is_approved]", 1, true) %>
>
> Rendered output:
> <input checked="checked" id="person[7][is_approved]"
> name="person[7][is_approved]" value="1" type="checkbox">
>
> Problem:
> The value of "1" is always passed, even if the box is UNCHECKED.
> Suggestions? Thanks!

http://www.ruby-forum.com/topic/60783


--
Michael Wang

Taylor Strait

unread,
Mar 30, 2007, 11:12:47 AM3/30/07
to rubyonra...@googlegroups.com

The reason I cannot use "check_box" is that I am passing an array of
values and check_box messes up the names: name="person[7][is_approved]"
becomes name="person[[7][is_approved]]"

Russell Norris

unread,
Mar 30, 2007, 11:35:16 AM3/30/07
to rubyonra...@googlegroups.com
You'll have to add your own hidden checkbox input then. There's no other way around it really. Sometimes it helps to write your own helper for it though. One less thing to remember and it's DRY.

RSL

Taylor Strait

unread,
Mar 30, 2007, 12:25:38 PM3/30/07
to rubyonra...@googlegroups.com
:index fixed this. Hooray for undocumented features!

Russell Norris

unread,
Mar 30, 2007, 12:56:49 PM3/30/07
to rubyonra...@googlegroups.com
:index fixed it? Care to explicate or should I just dig in the source? [Hint, hint.] :)

RSL

On 3/30/07, Taylor Strait < rails-mai...@andreas-s.net> wrote:

Taylor Strait

unread,
Mar 30, 2007, 1:28:17 PM3/30/07
to rubyonra...@googlegroups.com
For an array of users with boolean attributes here is how I iterated:

for person in @people

<%= check_box('person', 'is_approved', options = {:index => person.id,
:checked => person.is_approved}, checked_value = "1", unchecked_value =
"0") %>

Russell Norris

unread,
Mar 30, 2007, 2:28:32 PM3/30/07
to rubyonra...@googlegroups.com
Cool.

RSL

On 3/30/07, Taylor Strait <rails-mai...@andreas-s.net> wrote:

Nathan Garza

unread,
Apr 12, 2007, 2:17:51 PM4/12/07
to rubyonra...@googlegroups.com
Very cool.  I've needed something like that before.  Wish I'd known that then!

--

Nathan Garza

AshLeaf Media | Director of Technology Innovations
__________________________________________________________
www.ashleafmedia.com | nat...@ashleafmedia.com | 832.514.5726

Liana

unread,
Apr 19, 2007, 5:23:54 PM4/19/07
to rubyonra...@googlegroups.com
Taylor Strait wrote:
> For an array of users with boolean attributes here is how I iterated:
>
> for person in @people
>
> <%= check_box('person', 'is_approved', options = {:index => person.id,
> :checked => person.is_approved}, checked_value = "1", unchecked_value =
> "0") %>

Hrm. I've got multiple rows each with a checkbox on my page. I need to
know for which rows the user checks the box.

I got this to work:

check_box_tag('person[][is_approved]', person.is_approved)

But ran into the "unchecked" problem. So I was excited to find this
thread... but when I tried this:

<%= check_box('person', 'is_approved', options = {:index => person.id,
:checked => person.is_approved}, checked_value = "1", unchecked_value =
"0") %>

I got the error below:

"Conflicting types for parameter containers. Expected an instance of
Array, but found an instance of Hash. This can be caused by passing
Array and Hash based paramters qs[]=value&qs[key]=value."

So, I tried this:

<%= check_box('person[]', 'is_approved', options = {:index => person.id,
:checked => person.is_approved}, checked_value = "1", unchecked_value =
"0") %>

And now I get this error:

"object[] naming but object param and @object var don't exist or don't
respond to id_before_type_cast"

Any ideas?

Thanks!

Liana

unread,
Apr 20, 2007, 1:17:47 PM4/20/07
to rubyonra...@googlegroups.com


Okay, just for anyone who's interested. I got this to work by using
check_box_tag and the hidden field.

<%= people.each do |person| %>
<%= check_box_tag('person[][is_approved]', 1, 0)%>
<%= hidden_field_tag 'person[][is_approved]', 0 %>
<%= end %>

What was interesting about this is that you NEED to put the
hidden_field_tag BELOW the check_box_tag or else the values got out of
synch. In other words, I would check row #5 but when I submit the page,
it was row #6 that had the check mark.

Cheers!
Liana

Reply all
Reply to author
Forward
0 new messages