Created
May 21, 2010 17:12
-
-
Save reu/409109 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- form_for @products do |form| | |
%h3== Specifications | |
%ul#specifications_container | |
- form.fields_for :specifications do |specification_form| | |
= render :partial => "specification", :locals => { :form => specification_form } | |
%p= add_child_link "Add Specification", :specifications | |
= new_child_fields_template form, :specifications | |
%p= form.submit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
$('form a.add_child').click(function() { | |
var association = $(this).attr('data-association'); | |
var template = $('#' + association + '_fields_template').html(); | |
var regexp = new RegExp('new_' + association, 'g'); | |
var new_id = new Date().getTime(); | |
$("#" + association + "_container").append(template.replace(regexp, new_id)); | |
return false; | |
}); | |
$('form a.remove_child').live('click', function() { | |
var hidden_field = $(this).prev('input[type=hidden]')[0]; | |
if(hidden_field) { | |
hidden_field.value = '1'; | |
} | |
$(this).parents('.fields').hide(); | |
return false; | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ApplicationHelper | |
def new_child_fields_template(form_builder, association, options = {}) | |
options[:object] ||= form_builder.object.class.reflect_on_association(association).klass.new | |
options[:partial] ||= association.to_s.singularize | |
options[:form_builder_local] ||= :form | |
content_for :javascript_templates do | |
content_tag :div, :id => "#{association}_fields_template", :style => "display: none" do | |
form_builder.fields_for(association, options[:object], :child_index => "new_#{association}") do |f| | |
render(:partial => options[:partial], :locals => { options[:form_builder_local] => f }) | |
end | |
end | |
end | |
end | |
def add_child_link(name, association) | |
link_to name, "#", :class => "add_child", :"data-association" => association | |
end | |
def remove_child_link(name, form_builder) | |
form_builder.hidden_field(:_destroy) + link_to(name, "#", :class => "remove_child") | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!!! XML | |
!!! Strict | |
%html | |
%head | |
= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" | |
= javascript_include_tag "application" | |
%body | |
= yield | |
#javascript_templates{:style => "display: none"} | |
= yield :javascript_templates |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment