diff --git a/lib/nested_form/builder_mixin.rb b/lib/nested_form/builder_mixin.rb
index 26a98877..5f31b0be 100644
--- a/lib/nested_form/builder_mixin.rb
+++ b/lib/nested_form/builder_mixin.rb
@@ -104,7 +104,9 @@ def fields_for_nested_model(name, object, options, block)
private
def fields_blueprint_id_for(association)
- assocs = object_name.to_s.scan(/(\w+)_attributes/).map(&:first)
+ assocs = []
+ assocs << [object_name.to_s.split("[")[0]] # root
+ assocs.concat(object_name.to_s.scan(/(\w+)_attributes/).map(&:first)) # parent associations
assocs << association
assocs.join('_') + '_fields_blueprint'
end
diff --git a/spec/nested_form/builder_spec.rb b/spec/nested_form/builder_spec.rb
index 98456b72..6455b986 100644
--- a/spec/nested_form/builder_spec.rb
+++ b/spec/nested_form/builder_spec.rb
@@ -19,9 +19,9 @@
describe '#link_to_add' do
it "behaves similar to a Rails link_to" do
- subject.link_to_add("Add", :tasks).should == 'Add'
- subject.link_to_add("Add", :tasks, :class => "foo", :href => "url").should == 'Add'
- subject.link_to_add(:tasks) { "Add" }.should == 'Add'
+ subject.link_to_add("Add", :tasks).should == 'Add'
+ subject.link_to_add("Add", :tasks, :class => "foo", :href => "url").should == 'Add'
+ subject.link_to_add(:tasks) { "Add" }.should == 'Add'
end
it 'raises ArgumentError when missing association is provided' do
@@ -115,7 +115,7 @@
output.should match(/div.+data-blueprint="#{expected}"/)
end
- it "adds parent association name to the blueprint div id" do
+ it "adds parent associations to the blueprint div id" do
task = project.tasks.build
task.milestones.build
subject.fields_for(:tasks, :builder => builder) do |tf|
@@ -123,7 +123,7 @@
tf.link_to_add('Add', :milestones)
end
output = template.send(:after_nested_form_callbacks)
- output.should match(/div.+id="tasks_milestones_fields_blueprint"/)
+ output.should match(/div.+id="item_tasks_milestones_fields_blueprint"/)
end
it "doesn't render wrapper div" do