Nested lists of options

The situation is the following: you have a model and this model has_many :options which are actually only a text field.

How do you do a edit/new form for this model where you can edit any number of options? That includes handling of deletion of exisiting options and generation of new options. How to do the JS for the dynamic extension of the form with additional textfields for the new options?

What to do when the nested model is more complex?

This is a little annoying to do. Fortunately there’s a good RailsCast on it:

I second the rails cast suggested by Ben, but if you’re a RailsCasts Pro subscriber, there’s also a revised edition of that screencast that is more recent.

I’ve also used the cocoon gem in a previous project to take care of the dynamic addition/deletion javascript code.

1 Like