fields_for
lets you specify a particular object to render the fields for, so if you want your nested_fields
partial to contain nested fields for a single, newly build nested2
model, you can do it in the fields_for
call itself, like this:
# '_nested_fields.html.erb'
...
<%= f.fields_for :nested2, f.object.build_nested2 do |build| %>
<%= ... %>
<% end %>
This is assuming that Nested
has_one :nested2
, if it's a has_many
association the fields_for
arguments would be slightly different:
<%= f.fields_for :nested2s, f.object.nested2s.build do |build| %>
f.object
allows you to access the form builder's object, and you can then use it's association methods (based on the association type) to build the new object at that point.