Skip to content

What are nested forms and why a new gem ?

Louis Sommer edited this page Jun 18, 2021 · 6 revisions

Nested forms (or more accurately nested fields or nested attributes) are forms that deal with associated models. Let's picture a Project model that has_many :tasks. A nested form will allow you to create a project along with one or several tasks within a single form. If Tasks were to have associations on their own, like :comments, you could also, still in the same form, instantiate comments along with their parent models.

Rails provides its own helper to handle nested attributes. abyme is basically a smart wrapper around it, offering easier syntax along with some fancy additions. To work properly, some configuration will be required in both models and controllers (see our step by step tutorial).

What Rails doesn't provide natively is the possibility to dynamically add new associations on the fly, which requires Javascript implementation. What this means it that you would normally have to know in advance how many fields you'd like to display (1, 2 or any number of :tasks), which isn't very usable in this day and age. This is what the cocoon gem has been helping with for the past 7 years. This gem still being implemented in JQuery (which Rails dropped as a dependency), we wanted to propose a more plug'n'play approach, using Hotwire Stimulus instead.

Here are some useful resources we used for this gem:

Clone this wiki locally