From 657e4788016872a49121ab63adc3ad35900d1e1d Mon Sep 17 00:00:00 2001 From: Andrius Chamentauskas Date: Tue, 15 Mar 2011 16:41:17 +0200 Subject: [PATCH] Template is now saved under data-association attribute to prevent those extra blank records when submitting form. --- lib/cocoon/view_helpers.rb | 7 ++----- lib/generators/cocoon/install/templates/cocoon.js | 2 +- spec/cocoon_spec.rb | 12 ++++++------ 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/lib/cocoon/view_helpers.rb b/lib/cocoon/view_helpers.rb index 213edeb..be78f2a 100644 --- a/lib/cocoon/view_helpers.rb +++ b/lib/cocoon/view_helpers.rb @@ -61,12 +61,9 @@ module Cocoon html_options[:'data-association'] = association.to_s.singularize new_object = f.object.class.reflect_on_association(association).klass.new - model_name = new_object.class.name.underscore - hidden_div = content_tag('div', :id => "#{model_name}_fields_template", :style => "display:none;") do - render_association(association, f, new_object) - end + html_options[:'data-template'] = CGI.escapeHTML(render_association(association, f, new_object)) - hidden_div.html_safe + link_to(name, '#', html_options ) + link_to(name, '#', html_options ) end end diff --git a/lib/generators/cocoon/install/templates/cocoon.js b/lib/generators/cocoon/install/templates/cocoon.js index e0b6d25..6e1f64c 100644 --- a/lib/generators/cocoon/install/templates/cocoon.js +++ b/lib/generators/cocoon/install/templates/cocoon.js @@ -2,7 +2,7 @@ $(document).ready(function() { $('.add_fields').live('click', function() { var assoc = $(this).attr('data-association'); - var content = $(this).siblings('#' + assoc + '_fields_template').html(); + var content = $(this).attr('data-template'); var insertionPosition = $(this).attr('data-association-insertion-position'); var insertionNode = $(this).attr('data-association-insertion-node'); var regexp_braced = new RegExp('\\[new_' + assoc + '\\]', 'g'); diff --git a/spec/cocoon_spec.rb b/spec/cocoon_spec.rb index 7ad6614..b88bbac 100644 --- a/spec/cocoon_spec.rb +++ b/spec/cocoon_spec.rb @@ -15,18 +15,18 @@ describe Cocoon do @tester = TestClass.new @post = Post.new @form_obj = stub(:object => @post) - @tester.stub(:render_association).and_return('form') + @tester.stub(:render_association).and_return('form') end context "without a block" do it "should accept a name" do result = @tester.link_to_add_association('add something', @form_obj, :comments) - result.to_s.should == 'add something' + result.to_s.should == 'add something' end it "should accept html options and pass them to link_to" do result = @tester.link_to_add_association('add something', @form_obj, :comments, {:class => 'something silly'}) - result.to_s.should == "
form
add something" + result.to_s.should == 'add something' end end @@ -35,14 +35,14 @@ describe Cocoon do result = @tester.link_to_add_association(@form_obj, :comments) do "some long name" end - result.to_s.should == "
form
some long name" + result.to_s.should == 'some long name' end it "should accept html options and pass them to link_to" do result = @tester.link_to_add_association(@form_obj, :comments, {:class => 'floppy disk'}) do "some long name" end - result.to_s.should == "
form
some long name" + result.to_s.should == 'some long name' end end @@ -86,4 +86,4 @@ describe Cocoon do end end -end \ No newline at end of file +end