# Ajax complex forms: checkbox weirdness

**URL:** <https://www.ruby-forum.com/t/ajax-complex-forms-checkbox-weirdness/150576>\
**Category:** Rails\
**Created:** [October 10, 2008, 4:30am UTC](https://www.ruby-forum.com/t/ajax-complex-forms-checkbox-weirdness/150576 "2008-10-10T04:30:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [October 10, 2008, 4:30am UTC](https://www.ruby-forum.com/t/ajax-complex-forms-checkbox-weirdness/150576/1 "2008-10-10T04:30:47Z")

</div>

Hello!

Question for you wizards…

If you’ve used Ryan B. tutorial on complex forms:

> **[\#75 Complex Forms Part 3 - RailsCasts](http://railscasts.com/episodes/75-complex-forms-part-3)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

… or better yet, the one from Advanced Rails Recipes… perhaps you  
can help me. I set up a complex form for an Account that has many  
Formats which works nearly the same as the ARR example of Project  
has\_many Tasks. One thing that is breaking, though, is the use of  
checkboxes in the Formats partial.

The problem is that when I add a partial, Rails puts the hidden  
checkbox with value “0” in there… but this then gets passed in as  
another attribute set, so my virtual attribute methods bomb. For  
example, if I have a Format that has: Name (string), Description  
(text) and Active (boolean), then when I submit the form, I should  
get:

params[:account][:new\_format\_attribute] = [{:name =\>  
“foo”, :description =\> “bar”, :active =\> “1”}]

Instead, I get:

params[:account][:new\_format\_attribute] = [{:name =\>  
“foo”, :description =\> “bar”, :active =\> “1”}, {:active =\> “0”}]

You see? It’s adding the hidden value of active in as a separate  
attribute hash.

Any ideas on how to skip this? Or should I just skip the Rails  
checkbox helper and code my own?

Thanks for any tips!

-Danimal

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [October 10, 2008, 4:31am UTC](https://www.ruby-forum.com/t/ajax-complex-forms-checkbox-weirdness/150576/2 "2008-10-10T04:31:58Z")

</div>

BTW, it’s actually:

params[:account][:new\_format\_attributes] = …

(“attributes” not “attribute”)… that was just my poor typing.

Thanks,

-Danimal

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [October 10, 2008, 10:27pm UTC](https://www.ruby-forum.com/t/ajax-complex-forms-checkbox-weirdness/150576/3 "2008-10-10T22:27:22Z")

</div>

I don’t suppose it’s as simple as not calling the check\_box helper from  
the form object you’re getting out of fields\_for is it? I would guess  
that’s what would happen if you did…

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [October 11, 2008, 2:00am UTC](https://www.ruby-forum.com/t/ajax-complex-forms-checkbox-weirdness/150576/4 "2008-10-11T02:00:32Z")

</div>

On Oct 10, 9:26 pm, “Pardee, Roy” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I don’t suppose it’s as simple as not calling the check\_box helper from the form object you’re getting out of fields\_for is it? I would guess that’s what would happen if you did…

Nope, it’s a known complication of the way the check\_box helper works:  
it doesn’t mix well with arrays.  
either use check\_box\_tag (but lose the convenience of having 0  
submitted when the check\_box is unchecked) or use a hash rather than  
an array.

Fred

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [October 11, 2008, 2:04am UTC](https://www.ruby-forum.com/t/ajax-complex-forms-checkbox-weirdness/150576/5 "2008-10-11T02:04:38Z")

</div>

_sigh_ I wish there was an option on the helper. Oh well. I ended up  
leaving things as-is on the view and just handling it on the model.  
It’s not ideal, but it works. Basically, as I read through the array  
of attribute hash sets, I only process them if it’s a full set…  
i.e., in the above case, if there is no key :name (which is required),  
then I skip processing the hash.

Eventually, I might consider a better solution… perhaps overriding  
the checkbox helper to provide an option to suppress the hidden tag or  
something like that.

-Danimal

On Oct 10, 6:00 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
