A Rails gem to conveniently manage multiple nested models in a single form. It does so in an unobtrusive way through jQuery.
This has been updated. To learn more about how originally worked under the hood: blog.madebydna.com/dynamic-nested-forms-in-rails-3-with-the-nest
I have modified this to allow the use of nested_form as a table view. This has changed the the underlying implementation by:
The container <div class=“fields”> is no longer automatically generated. you are assumed to generate your own entry container with that proper class “fields”. as show in the updated usage section below.
Add it to your Gemfile
gem "nested_form", :git => "git://github.com/modsaid/nested_form.git"
Run the generator
rails generate nested_form:install
You can then generate a nested form using the nested_form_for helper method.
<%= nested_form_for @project do |f| %>
Use this form just like normal, including the fields_for helper method for nesting models. The benefit of this plugin comes from the link_to_add and link_to_remove helper methods on the form builder.
<table> <%= f.fields_for :tasks do |task_form| %> <tr class="fields"> <td><%= task_form.text_field :name %></td> <td><%= task_form.link_to_remove "Remove this task" %></td> <tr> <% end %> <tr> <td><%= f.link_to_add "Add a task", :tasks %></td> </tr> </table>
This generates links which dynamically add and remove fields.
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.
<%= 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.
This gem is based on the plugin by Ryan Bates: github.com/ryanb/nested_form