diff --git a/README.md b/README.md index 667b9dec..12c7f244 100644 --- a/README.md +++ b/README.md @@ -81,13 +81,19 @@ 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 %> ``` -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 diff --git a/lib/nested_form/builder_mixin.rb b/lib/nested_form/builder_mixin.rb index 26a98877..23b3dace 100644 --- a/lib/nested_form/builder_mixin.rb +++ b/lib/nested_form/builder_mixin.rb @@ -73,7 +73,7 @@ def link_to_remove(*args, &block) 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}) } + block = args.pop options = args.dup.extract_options! @@ -81,6 +81,10 @@ def fields_for_with_nested_attributes(association_name, *args) 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 }