Multi Step & Nested Forms
This article will run through two common patterns in Rails apps: multi step forms, and forms with nested relationships.
We’ve found that while these patterns are reasonably straightforward when you know how, they’re not obvious. So this tutorial will go through putting them together, step by step.
Set Up The Parent Table
The parent table is the table that carries the user through whatever multi step process they’re completing. Whenever a user starts a new submission, our process is as follows:
-
Create a new record in the parent table.
-
Generate a shortcode for that record.
-
Pass this shortcode from step to step throughout the process.
For the purposes of this tutorial, let’s imagine our parent table is the Order
model. First we’ll add a new column to the table
Next, we make sure a record always has a shortcode.
Side Note: Conditional Validations
ActiveRecord validations are great, but usually they’re applied universally, not conditionally. For this flow, we want to have a way to switch between different “sets” of validations, depending on what step the user is on.
Now in our controller, we can do
Step One: Creating the record
For the first step of the flow, the user will simply click on a link and get taken to step one of a form.
Now, we can add a simple link to the app. Any time a user clicks on it, a new order will be created and they’ll be redirected to the first step.
Building The Form
Now lets add the route that shows the first step. We’re going to use :match
here for reasons we’ll explore in a few minutes.
Get the relevant order record when the user hits step one.
Build the form that gets shown when the user visits step one
Form submission and showing validation errors.
The view above will display the form to the user and let them fill it in. But we also want to cover the scenario where the user has submitted the form but some of the validations haven’t passed.
Update the controller action. If a user submits the form (post request), validate and save the changes, then redirect to step2.
To recap, the above code will handle three different scenarios.
-
The user has landed on the first step of the form but not submitted it yet. Just show them the form.
-
The user has submitted the form and there are validation errors. Show them the form as well as the validation errors.
-
The user has submitted the form and the validation has passed. Redirect them to the next step
The same pattern can be repeated for each step of the form.
Nested Relationships
Use a normal link to add an item
Add a route for it
And a controller action
Build your form using the fields_for
form helper
Sometimes you might only want to show inputs for a subset of the records.