Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Add support for partial option when no block is used with fields_for #310

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/nested_form/builder_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,18 @@ 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!

# 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 }
Expand Down