Parents, Children & Grandchildren in one form

0 views
Skip to first unread message

pimpmaster

unread,
Feb 20, 2008, 2:00:27 PM2/20/08
to attribute_fu
This may be a total pipe dream on my part, but just humor me for a
second.

:group has_many :people, :attributes => true
:person has_many :phones, :attributes => true

- form_for :group
= f.render_associated_form(@group.people, :new => 1)

Typical stuff so far, but what happens when we try to add phones in
the same manner inside that form partial?

= f.text_field :full_name
= f.remove_link('Remove')
= f.render_associated_form(@person.phones, :new => 1)

Your HTML breaks, that's what happens! It is no longer valid when you
have multiple IDs of the same name, plus the add link is not smart
enough to figure out which ID you are referring to, so it will add all
the phone numbers to the 1st person, even if you hit the link in the
3rd or 4th.

Is this something that could be fixed by simply using a different
prototype selector?

Something like $(this).next('.phone') ??

Or would more work need to be done on the AR side of the equation.

Hmmmmmm...

James Golick

unread,
Feb 20, 2008, 7:30:47 PM2/20/08
to attrib...@googlegroups.com
/me runs and hides.

Does the f.render_associated_form even create the right names? blah[something_attributes][0][something_else_attributes][0] ?

Pastie some generated HTML. I'm curious.

J.

pimpmaster

unread,
Feb 20, 2008, 8:35:15 PM2/20/08
to attribute_fu
Actually I think att_fu does an admirable job of spitting out the
right HTML.

Using different models than those specified above

:design has_many :color_ways
:color_way has_many :sizes

In my r_c controller for designs:

new_action.before { @color_way = @design.color_ways.build }

And the views:

_form.html.haml

#color_ways
%h2 Color Ways
%fieldset
= f.render_associated_form(@object.color_ways, :new => 1)
= f.add_associated_link('Add Color Way', @object.color_ways.build

_color_way.html.haml

%p.color_way
%label Name
= f.text_field :name
#sizes
= f.render_associated_form(@color_way.sizes, :new => 1)
= f.add_associated_link('Add Size', @color_way.sizes.build)

_size.html.haml

%p.size
%label Size Name
= f.text_field :name

Here is the generated HTML

http://pastie.textmate.org/private/aia8qdztvog1aufohkguw

Now this is obviously me really bastardizing the hell out of your
plugin and I'll understand if this is something that Just Wont Work®.
But it is interesting to see just how far things can be pushed and I
have a feeling this may not be as complicated as it seems.

Take a look and let me know if I need to lay off the dream pipes. :p

James Golick

unread,
Feb 20, 2008, 8:58:44 PM2/20/08
to attrib...@googlegroups.com
Wow! It works!!!

Just do this to get the rest of the stuff working: name the phones container like dom_id(@person)_phones # => person_23_phones

and then in the partial, pass the :container flag to add_associated_link('Add Phone', @person.phones.build, :container => "#{dom_id(@person)}_phones") <-- but make a helper for that

That should do the trick... keep me posted - this is a feat for attribute_fu!

pimpmaster

unread,
Feb 20, 2008, 9:16:27 PM2/20/08
to attribute_fu
Doh!

Called id for nil, which would mistakenly be 4 -- if you really wanted
the id of nil, use object_id

Makes sense, since @color_way = @design.color_ways.build, it doesn't
appear to have an id assigned to it yet.

Maybe we just need a generic way to auto-increment those dom ids...

pimpmaster

unread,
Feb 20, 2008, 9:19:38 PM2/20/08
to attribute_fu
Wait! I stand corrected. there was a typo on an instance variable and
this now appears to be working.

There is one little detail though.. new instances of person generates
2 phones instead of the :new => 1 that I specified.

Need to test this a bit more, but its looking promising!

pimpmaster

unread,
Feb 20, 2008, 9:22:53 PM2/20/08
to attribute_fu
Argh.. its still adding to the first container.

Looking at the source, I see that dom_id(@person)_phones is just
returning

new_person_phones

I can think of a couple of ways to get around this, but I need some
rest!

btw- look outside if its a clear night. There's a lunar eclipse
happening right now

James Golick

unread,
Feb 20, 2008, 9:43:44 PM2/20/08
to attrib...@googlegroups.com
If you want, pastie your view code, and I'll give you a hand with it. I'm sure we can get this working.

pimpmaster

unread,
Feb 21, 2008, 7:40:32 AM2/21/08
to attribute_fu

James Golick

unread,
Feb 21, 2008, 9:20:52 AM2/21/08
to attrib...@googlegroups.com
In the color_way partial, I think it should be color_way without the @ sign.

That's the only think I can see (though, I am pre-caffeine - will look more later :))

J.

On Thu, Feb 21, 2008 at 7:40 AM, pimpmaster <steven...@gmail.com> wrote:

http://pastie.textmate.org/private/qk115thmsn9ojsq59kmg


pimpmaster

unread,
Feb 21, 2008, 9:28:41 AM2/21/08
to attribute_fu
Same result:

new_color_way_sizes

There has to be a way to assign IDs to these new instances, though for
the life of me I cant figure out how.

James Golick

unread,
Feb 21, 2008, 9:34:04 AM2/21/08
to attrib...@googlegroups.com
Ah, because it is a new instance, right (sorry, I'm blaming caffeine).

They actually are already numbered by attribute fu, but it's getting at that number that's tricky.

Let me think about this a bit, and get back to you.

pimpmaster

unread,
Feb 21, 2008, 9:47:02 AM2/21/08
to attribute_fu
LOL.. Do what you gotta do.

Im gonna make some green tea (trying to kick my coffee habit)

James Golick

unread,
Feb 21, 2008, 10:29:38 AM2/21/08
to attrib...@googlegroups.com
Don't kick your coffee habit. Coffee is great, and tea is just awful, watery garbage. I am coming out in this mailing list post as anti-tea.

I'm just kidding of course (sorta), but back to the serious stuff...

You were right originally to suggest an alternate CSS selector. So, I just committed a revision to trunk whereby you can give you div a class of .sizes, and pass :expression => "$(this).prev(.sizes)" (might be this.up, depending on your DOM of course) to add_associated_link, and it should select the correct container for you (or you can alter that expression to do whatever, obviously).

Let me know how you make out.

James

pimpmaster

unread,
Feb 21, 2008, 11:34:16 AM2/21/08
to attribute_fu
I actually like Green tea.. but will concede that a proper espresso is
inimitable ;)

Upgraded to trunk and seem to be having selector problems:

http://pastie.textmate.org/private/8nhv0yvqok7c5unm57zjag

This is probably more my fault than yours as I'm still getting the
hang of CSS selectors in prototype

James Golick

unread,
Feb 21, 2008, 11:41:54 AM2/21/08
to attrib...@googlegroups.com
$(this).previous('.sizes')

pimpmaster

unread,
Feb 21, 2008, 1:32:02 PM2/21/08
to attribute_fu
Congrats James, It's working beautifully!

There is only one small detail left.. the remove links.

The Size is getting removed correctly, but the color way is only
removing the fields for color ways.

Maybe we need that :expression option for the remove link?

James Golick

unread,
Feb 21, 2008, 2:54:01 PM2/21/08
to attrib...@googlegroups.com
What do you want the color way remove link to do?

pimpmaster

unread,
Feb 21, 2008, 3:05:18 PM2/21/08
to attribute_fu
I actually got it working like I wanted to, just moved the class
selector to a different DOM element and *POOF* it works like magic.

Thanks for making these additions.. the plugin kicks ten times more
ass than it already did!

James Golick

unread,
Feb 21, 2008, 3:07:41 PM2/21/08
to attrib...@googlegroups.com
That's awesome... children, and grandchildren. Chalk one up for attribute_fu.

I do think it's a good idea to add an expression option to remove link too, and I'll do that soon.

Thanks for all the feedback and suggestions!

J.

pimpmaster

unread,
Feb 21, 2008, 3:17:48 PM2/21/08
to attribute_fu
My pleasure!

One more tiny request while you are reworking that helper..

:class => 'my_dom_class', :id => 'my_dom_id'

With that in place this plugin will be pretty much perfect

pimpmaster

unread,
Feb 21, 2008, 8:15:35 PM2/21/08
to attribute_fu
Actually scratch that, it's working as expected.

What else can I say?

Your plugin gets the official Pimpmaster Stamp of Approval

http://railsforum.com/viewtopic.php?pid=54888#p54888

James Golick

unread,
Feb 21, 2008, 8:16:55 PM2/21/08
to attrib...@googlegroups.com
awesome... thanks :)

pimpmaster

unread,
Feb 23, 2008, 11:17:08 PM2/23/08
to attribute_fu
Just ran across a little wrinkle here.

The validation on grandchildren is totally skipped.

I can put validates_presence_of :name

but that will still save grandchildren with blank names.

Give it a shot and let me know if you run across the same issue.

James Golick

unread,
Feb 24, 2008, 9:20:31 PM2/24/08
to attrib...@googlegroups.com
Hey,

Ive been trying to find time to get back to this, but I'm out of town for the next week, and I'm not sure ill be able to before I get back.

If you dont hear from me by next week sometime, maybe send me a reminder email :)

J

pimpmaster

unread,
Feb 25, 2008, 8:33:42 AM2/25/08
to attribute_fu
No prob dude.

Bon Voyage!

pimpmaster

unread,
Feb 25, 2008, 9:39:34 PM2/25/08
to attribute_fu
Actually dont even worry, I figured out the problem

I had :discard_if => :blank? on an attribute that I was also
validating.. obviously you cant do both ;)

Right now children and grandchildren are validating as they should,
thanks to error_message_on

Mahip

unread,
Apr 1, 2008, 6:30:20 AM4/1/08
to attribute_fu
hi
Can you please illustrate a proper working example for parent, child
and grandchild relations using att_fu, as the above discussion seems a
bit confusing.
Though, i tried that, but got some issues while adding grandchildren
relation.

thanx

James Golick

unread,
Apr 1, 2008, 8:42:16 AM4/1/08
to attrib...@googlegroups.com
I don't have an example, so I'll have to defer to pimpmaster if he's around.

J.

mcannon

unread,
Apr 4, 2008, 4:51:24 PM4/4/08
to attribute_fu
I'll bet a screencast showing an example of using attribute_fu to
implement parents-children-grandchildren in a single form would be a
huge hit. I have been trying to find an example of using a single
form to manage multiple models with parents-children-grandchildren
like this in every rails forum and book I can find and haven't found a
single useable example. It sounds like pimpmaster made it work with
attribute_fu. The closest thing I have found are Ryan B screen casts
but they only cover a parent and a child relation.

If anyone has seen a working example of code implementing parents-
children-grandchildren in a single form I would really appreciate
posting a link here.

Thanks,
- Mike

James Golick

unread,
Apr 4, 2008, 4:53:39 PM4/4/08
to attrib...@googlegroups.com
Maybe I'll make one when I have a chance.

Have you seen my a_fu screencast? It's just an intro, but it uses the plugin, unlike Ryan's: http://jamesgolick.com/2008/1/28/look-out-ryan-bates-there-s-a-new-screencast-in-town

mcannon

unread,
Apr 4, 2008, 5:24:43 PM4/4/08
to attribute_fu
That would be awesome if you did make a screencast on this topic - and
yes, I have seen your a_fu screencast and I thought it was great. It
is a very clean solution for implementing parents and children in a
single form among other things. I just need to figure out how to use
a_fu for parents-children-grandchildren in a single form and was
hoping you or pimpmaster could point me in the right direction.

Thanks again,


James Golick

unread,
Apr 4, 2008, 5:27:26 PM4/4/08
to attrib...@googlegroups.com
np.

I've personally never tried it, so maybe if the pimpmaster is around, he can chime in, or if I've got time this weekend, maybe I'll make that cast :)

J.

pimpmaster

unread,
Apr 9, 2008, 2:33:39 PM4/9/08
to attribute_fu
Annnnnnnd there it is!

http://pastie.textmate.org/163163

Enjoy!

James Golick

unread,
Apr 9, 2008, 2:34:34 PM4/9/08
to attrib...@googlegroups.com
thanks pimpmaster!

mcannon

unread,
Apr 10, 2008, 12:48:23 PM4/10/08
to attribute_fu
You da man!

Thanks for sharing.

Mahip

unread,
Apr 11, 2008, 4:51:18 AM4/11/08
to attribute_fu
Thank u :)
Reply all
Reply to author
Forward
0 new messages