Topic: build nested models in form

I would like to expand on the application helper method introduced by Ryan Bates in his Railcast #197 by explicitly building the child models in the helper method. Please see below:

Where children is an array of association models passed to the helper method: e.g. [:addresses, :phones]

  def link_to_add_fields(name, f, association, children)
    new_object = f.object.class.reflect_on_association(association)                                             # This does work however it is not dynamic
    children.each do |child|                                                     # This loop does not work
    fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
      render(association.to_s.singularize + "_form", :f => builder)
    link_to_function(name, ("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))

Last edited by bennyzr (2012-05-02 16:05:43)