In this episode, we're adding the ability for users to switch between teams in our app.
We start by building a PATCH route that will use a controller to update the user's current team, using route model binding to figure out which team they're switching to. We set this up in a TeamController
and create a new setCurrent
method where all the switching logic will go. You'll notice we don't worry about authorization just yet — we'll tackle that in the next episode.
To actually trigger the switch from the UI, we build a small form for each team in the dropdown navigation. Since we don't want to use a simple GET request for this (security reasons!), we make a form that does a PATCH request, using method spoofing and including the CSRF token. When the user clicks to switch, this form is submitted and hits the new route.
Once that's wired up, we go back to the controller and, when the request comes in, we update the user's current team to the one provided. We quickly confirm this works by switching in the UI and watching the changes happen, then write a feature test to prove everything is behaving as expected (super useful for when we add authorization later, too).
By the end of this episode, users can switch teams with a click, both in the UI and through a properly-tested backend. Next up: making sure only valid team members can make the switch!