-
Notifications
You must be signed in to change notification settings - Fork 3
What are nested forms and why a new gem ?
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:
- Rails's official docs for nested attributes
- Rails's doc for fields_for
- The cocoon gem
- This Drifting Ruby tutorial by David Kimura