Thursday, June 17, 2010

[Rails] Nested Forms and ordering of new child elements

Hi everybody,

I am using Rails 2.3 and the new "nested forms" concept to add child
objects to a parent object from within a single form.

I have successfully gotten the example app from Github (alloy_complex-
form-examples) to run and modified the Javascript code (add_child) to
insert new child elements betwee existing children, _not_ only at the
end of the list of children. This works fine, client-side.

However, this order is not preserved. When I try to save the form, the
new children are simply appended to the list of children. Now I could
solve this by populating a "position" attribute using Javascript. But
when validation fails, the list of children is _also_ reordered before
Rails re-renders the form, so that new children always appear at the
end.

How do I define, within a form_for / fields_for construct, the ORDER
of child elements so that it will be preserved

* upon re-rendering the form (when validations fail or exeptions
occur)
* upon successfully saving the object

?

I have a "default_scope :order => :position" set in the child model,
which also works fine, but does not help in the first case.

How is the "accepts_nested_attributes_for" implemented, can I somehow
hook into this?

Thanks!

Jens

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate