How to create a form in Rails with a variable number of jsonb items?

31 views Asked by At

I have a model that stores modifiers for each year, for a variable number of years. It might be 0, it might be 10, it could be 50. The modifiers are not the same, and require different details depending on the type that they are.

The modifiers are only ever retrieved with the model, and don’t need to be sorted or queried or anything separately. Ideally, I’d like to just store them as JSON in one field on my model.

My question is how would I go about presenting a form that allows you to attach one or more modifiers to it? Basically I need the ability to add a different set of fields, depending on the type of modifier. And I need to do it repeatedly (like an add more button. It could either add one by type, or add one more generically with a select for the type)

I think this should be possible with Stimulus but I’m pretty lost how to do it on both the rails model/form side and the stimulus side.

0

There are 0 answers