Template is now saved under data-association attribute to prevent those extra blank records when submitting form.

This commit is contained in:
Andrius Chamentauskas 2011-03-15 16:41:17 +02:00
parent 2be9a44303
commit 657e478801
3 changed files with 9 additions and 12 deletions

View File

@ -61,12 +61,9 @@ module Cocoon
html_options[:'data-association'] = association.to_s.singularize html_options[:'data-association'] = association.to_s.singularize
new_object = f.object.class.reflect_on_association(association).klass.new new_object = f.object.class.reflect_on_association(association).klass.new
model_name = new_object.class.name.underscore html_options[:'data-template'] = CGI.escapeHTML(render_association(association, f, new_object))
hidden_div = content_tag('div', :id => "#{model_name}_fields_template", :style => "display:none;") do
render_association(association, f, new_object)
end
hidden_div.html_safe + link_to(name, '#', html_options ) link_to(name, '#', html_options )
end end
end end

View File

@ -2,7 +2,7 @@ $(document).ready(function() {
$('.add_fields').live('click', function() { $('.add_fields').live('click', function() {
var assoc = $(this).attr('data-association'); 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 insertionPosition = $(this).attr('data-association-insertion-position');
var insertionNode = $(this).attr('data-association-insertion-node'); var insertionNode = $(this).attr('data-association-insertion-node');
var regexp_braced = new RegExp('\\[new_' + assoc + '\\]', 'g'); var regexp_braced = new RegExp('\\[new_' + assoc + '\\]', 'g');

View File

@ -15,18 +15,18 @@ describe Cocoon do
@tester = TestClass.new @tester = TestClass.new
@post = Post.new @post = Post.new
@form_obj = stub(:object => @post) @form_obj = stub(:object => @post)
@tester.stub(:render_association).and_return('form') @tester.stub(:render_association).and_return('form<tag>')
end end
context "without a block" do context "without a block" do
it "should accept a name" do it "should accept a name" do
result = @tester.link_to_add_association('add something', @form_obj, :comments) result = @tester.link_to_add_association('add something', @form_obj, :comments)
result.to_s.should == '<div id="comment_fields_template" style="display:none;">form</div><a href="#" class="add_fields" data-association="comment">add something</a>' result.to_s.should == '<a href="#" class="add_fields" data-association="comment" data-template="form&amp;lt;tag&amp;gt;">add something</a>'
end end
it "should accept html options and pass them to link_to" do 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 = @tester.link_to_add_association('add something', @form_obj, :comments, {:class => 'something silly'})
result.to_s.should == "<div id=\"comment_fields_template\" style=\"display:none;\">form</div><a href=\"#\" class=\"something silly add_fields\" data-association=\"comment\">add something</a>" result.to_s.should == '<a href="#" class="something silly add_fields" data-association="comment" data-template="form&amp;lt;tag&amp;gt;">add something</a>'
end end
end end
@ -35,14 +35,14 @@ describe Cocoon do
result = @tester.link_to_add_association(@form_obj, :comments) do result = @tester.link_to_add_association(@form_obj, :comments) do
"some long name" "some long name"
end end
result.to_s.should == "<div id=\"comment_fields_template\" style=\"display:none;\">form</div><a href=\"#\" class=\"add_fields\" data-association=\"comment\">some long name</a>" result.to_s.should == '<a href="#" class="add_fields" data-association="comment" data-template="form&amp;lt;tag&amp;gt;">some long name</a>'
end end
it "should accept html options and pass them to link_to" do 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 result = @tester.link_to_add_association(@form_obj, :comments, {:class => 'floppy disk'}) do
"some long name" "some long name"
end end
result.to_s.should == "<div id=\"comment_fields_template\" style=\"display:none;\">form</div><a href=\"#\" class=\"floppy disk add_fields\" data-association=\"comment\">some long name</a>" result.to_s.should == '<a href="#" class="floppy disk add_fields" data-association="comment" data-template="form&amp;lt;tag&amp;gt;">some long name</a>'
end end
end end
@ -86,4 +86,4 @@ describe Cocoon do
end end
end end
end end