Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Steps
What is happening?
The create action in our
TopicsControllerlooks almost identical to an equivalent default Rails application. Inertia knows how to follow redirects, so a successful save is exactly the same. If there are errors, we need to pass the errors in theinertiaoption of the redirect. If we do that, the Inertia<Form /> componentwill have access to the Rails validation errors.The other interesting thing in the example is that we are saving multiple models at once. We require that a user adds the first message to the topic they are creating. To accomplish this, we use nested attributes and a Rails validation context. If we always validated that a topic had at least one message, our database seeds would break. So we are treating this form as a special case, rather than the default. This doesn't have anything to do with Inertia, its... just regular ol' Rails patterns! And that's the point. Because Inertia is built with all of the helpful MVC constraints in mind, it plays really nicely with existing Rails best practices.