From 27b0d6e5faaf6b41b1805347908dbae6f906cdbb Mon Sep 17 00:00:00 2001 From: Robert Bradford Date: Sun, 9 Feb 2014 16:02:22 -0700 Subject: [PATCH 1/4] Add ability to specify a partial option when calling f.fields_for --- lib/nested_form/builder_mixin.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/nested_form/builder_mixin.rb b/lib/nested_form/builder_mixin.rb index 26a98877..8a2bdb05 100644 --- a/lib/nested_form/builder_mixin.rb +++ b/lib/nested_form/builder_mixin.rb @@ -72,10 +72,11 @@ def link_to_remove(*args, &block) end def fields_for_with_nested_attributes(association_name, *args) - # TODO Test this better - block = args.pop || Proc.new { |fields| @template.render(:partial => "#{association_name.to_s.singularize}_fields", :locals => {:f => fields}) } - options = args.dup.extract_options! + options[:partial] ||= "#{association_name.to_s.singularize}_fields" + + # TODO Test this better + block = args.pop || Proc.new { |fields| @template.render(:partial => options[:partial], :locals => {:f => fields}) } # Rails 3.0.x if options.empty? && args[0].kind_of?(Array) From e6a821b276103e09c6eb0890f5f14bd534422f37 Mon Sep 17 00:00:00 2001 From: Robert Bradford Date: Sun, 9 Feb 2014 17:53:52 -0700 Subject: [PATCH 2/4] If no block is used in fields_for, allow for a :partial option or use the default partial. --- lib/nested_form/builder_mixin.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/nested_form/builder_mixin.rb b/lib/nested_form/builder_mixin.rb index 8a2bdb05..23b3dace 100644 --- a/lib/nested_form/builder_mixin.rb +++ b/lib/nested_form/builder_mixin.rb @@ -72,16 +72,19 @@ def link_to_remove(*args, &block) end def fields_for_with_nested_attributes(association_name, *args) - options = args.dup.extract_options! - options[:partial] ||= "#{association_name.to_s.singularize}_fields" - # TODO Test this better - block = args.pop || Proc.new { |fields| @template.render(:partial => options[:partial], :locals => {:f => fields}) } + block = args.pop + + options = args.dup.extract_options! # Rails 3.0.x if options.empty? && args[0].kind_of?(Array) options = args[0].dup.extract_options! end + + # Check for a :partial option or use the default + partial = options.delete(:partial) || "#{association_name.to_s.singularize}_fields" + block ||= Proc.new { |fields| @template.render(:partial => partial, :locals => {:f => fields}) } @fields ||= {} @fields[fields_blueprint_id_for(association_name)] = { :block => block, :options => options } From 37ef01d4f1f59befafa4514a3ea20967b1917ac1 Mon Sep 17 00:00:00 2001 From: Robert Bradford Date: Sun, 9 Feb 2014 18:01:10 -0700 Subject: [PATCH 3/4] Update partials documentation. --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 667b9dec..49328c36 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,13 @@ It is often desirable to move the nested fields into a partial to keep things or <%= f.fields_for :tasks %> ``` -In this case it will look for a partial called "task_fields" and pass the form builder as an `f` variable to it. +In this case it will look for a partial called "task_fields". You can also pass a specific partial as an option: + +```erb +<%= f.fields_for :tasks, partial: 'partial/path' %> +``` + +In either case the form builder will be passed as an `f` variable to the partial. ## Specifying a Target for Nested Fields From b1ba42fb78e87d6ac86911c2d1f77b9e59256447 Mon Sep 17 00:00:00 2001 From: Robert Bradford Date: Sun, 9 Feb 2014 18:05:47 -0700 Subject: [PATCH 4/4] Change wording in README. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 49328c36..12c7f244 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ Use `simple_nested_form_for` or `semantic_nested_form_for` for SimpleForm and Fo ## Partials -It is often desirable to move the nested fields into a partial to keep things organized. If you don't supply a block to fields_for it will look for a partial and use that. +It is often desirable to move the nested fields into a partial to keep things organized. If you don't supply a block to fields_for it will look for a partial to use. ```erb <%= f.fields_for :tasks %>