Nested Fields

nested_fields is an Engine designed for generic, dynamic nested forms which may be N>=1 levels deep.

Of what I could find, the existing solutions for dynamic nested forms in Rails are implemented with javascript rather through Controllers. It is my understanding that this is because FormBuilders cannot be passed as params to Controllers.

It does rely on some parsing behavior within Rails which may or may not be intended for use in this manner.

Requirements

Designed for Rails 3 applications.

jQuery is required. In the future, this requirement could be removed without much difficulty.

Install

Add nested_fields to the Gemfile and bundle install.

Usage

Include ‘nested_fields.js’ in the layout for the Add and Remove behaviors.

Use the nested_fields_for helper method.

<%= nested_fields_for form_builder, :pluralized_association_name, 'path_to/inner_partial' %>

The same type of the form builder argument (e.g. Formtastic, SimpleForm, etc.) will automatically be used for the nested fields.

The nested_fields_ready custom event is fired after a nested fieldset is (dynamically or otherwise) added.

Credits

Inspired by Ryan Bates’ nested_form which itself is based on Tim Riley’s solution.