RondoForm
Handle dynamic nested forms, same as Cocoon, but using StimulusJS
Installation
Install the gem and add to the application's Gemfile by executing:
$ bundle add rondo_form
Or inside the Gemfile add the following
$ gem 'rondo_form', '~> 0.2.6'
Run the installation task:
$ rails g rondo_form:install
Usage
For example, you have Project
model, which has has_many
relationship with Task
model:
rails g scaffold Project name:string description:string
rails g model Task description:string done:boolean project:belongs_to
You need to add accepts_nested_attributes_for
to Project
model:
class Project < ApplicationRecord
has_many :tasks, dependent: :destroy
accepts_nested_attributes_for :tasks, reject_if: :all_blank, allow_destroy: true
end
Sample with SimpleForm
The RondoForm gem adds two helper functions: link_to_add_association
and link_to_remove_association
.
The example below illustrates the way to use it.
In your projects/_form
partial:
<%= simple_form_for(@project) do |f| %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.input :description %>
</div
…