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 Hotwired set of frameworks that is well suited to this task: Stimulus.[58] 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 ...
Get Agile Web Development with Rails 7 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.