Skip to content

Instantly share code, notes, and snippets.

@guitsaru
Created July 24, 2010 03:00
Show Gist options
  • Save guitsaru/488349 to your computer and use it in GitHub Desktop.
Save guitsaru/488349 to your computer and use it in GitHub Desktop.
$('.action.new[data-content][data-association]').click(function () {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + $(this).attr('data-association'), "g");
$(this).before($(this).attr('data-content').replace(regexp, new_id));
return false;
});
def new_object_fields(f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
f.fields_for(association, new_object, :child_index => "new_#{association.to_s.singularize}") do |builder|
render(:partial => "#{association.to_s.singularize}_fields", :locals => {:f => builder})
end
end
= form_for @parent do |f|
%h3 Children
%fieldset.children
= fields_for :children do |children_form|
= render :partial => 'parent/children_fields', :locals => {:f => children_form}
= link_to 'Add Child', '#', :class => 'action new', 'data-association' => 'child', 'data-content' => new_object_fields(f, :children)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment