Forum: Ruby on Rails ajax complex forms: checkbox weirdness

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Danimal (Guest)
on 2008-10-10 06:30
(Received via mailing list)
Hello!

Question for you wizards...

If you've used Ryan B. tutorial on complex forms:
http://railscasts.com/episodes/75
... 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
Danimal (Guest)
on 2008-10-10 06:31
(Received via mailing list)
BTW, it's actually:

params[:account][:new_format_attributes] = ...

("attributes" not "attribute")... that was just my poor typing.

Thanks,

-Danimal
Roy P. (Guest)
on 2008-10-11 00:27
(Received via mailing list)
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...
Frederick C. (Guest)
on 2008-10-11 04:00
(Received via mailing list)
On Oct 10, 9:26 pm, "Pardee, Roy" <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
Danimal (Guest)
on 2008-10-11 04:04
(Received via mailing list)
*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>
This topic is locked and can not be replied to.