Skip to content

binarycode/nested_form

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nested Form

A Rails gem to conveniently manage multiple nested models in a single simple_form. It does so in an unobtrusive way through jQuery.

Note: this gem extends the simple_form gem (github.com/plataformatec/simple_form)

Install

Add it to your Gemfile

gem "nested_form", :git => "git://github.com/binarycode/nested_form.git"

Run

bundle install

Run the generator

rails generate nested_form:install

Usage

Running the generator will add a file at public/javascripts/nested_form.js which should be included after jQuery.

<%= javascript_include_tag 'jquery', 'nested_form' %>

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 simple_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.

<%= f.simple_fields_for :tasks do |task_form| %>
  <%= task_form.input :name %>
  <%= task_form.link_to_remove "Remove this task" %>
<% end %>
<%= f.link_to_add "Add a task", :tasks %>

This generates links which dynamically add and remove fields.

Partials

[Somehow this was not working with Ryan Bates’s initial gem but should work ok with this fork].

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.simple_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.

Additional Options

You can limit how many children a user can add by setting the :limit option :

<%= f.link_to_add "Add a task", :tasks, :limit => 2 %>

Special Thanks

This gem is based on the plugin by Ryan Bates: github.com/ryanb/nested_form

About

Rails plugin to conveniently handle multiple models in a single form.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 79.1%
  • JavaScript 20.9%
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy