Iteration G2: Adding Fields Dynamically to a Form
We need a dynamic form that changes what fields are shown based on what pay type the user has selected. We could cobble something together with jQuery, but Rails includes another framework from the Hotwire set of frameworks that is well suited to this task: Stimulus.[63] Let’s put it to use!
Creating a Stimulus Controller
Our starting point is clearly the existing order form. The plan is to add some additonal fields, cause those fields to be hidden on initial display, and finally, to expose the fields associated with selected pay type whenever the selection changes.
Let’s focus intially on the behavior we want to implement, then on the markup. With Stimulus, the behavior is placed inside a ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access