In this episode, we work through how to update a member's role in a team. We go step-by-step, starting by figuring out where our form should send its data—we decide on a PATCH request and set up the corresponding route in our application. With the route in place, we head into the Team Member Controller and add an update
method to handle the request.
From there, we set up a test for the best case scenario: updating a member's role from "member" to "admin". We walk through attaching a new user to a team in our tests, making sure they have the right role context, and patching their role. We explain how to properly assign roles inside tests using helper methods, and confirm with assertions that roles are being updated as expected (and not stacking multiple roles unless intended).
We also cover important edge cases by testing that roles are only updated when a valid role is provided—preventing accidental null or bogus updates. We attach and detach roles with care, ensuring that if a role is changed, the old one is dropped so there's only ever one role at a time unless multiple roles are part of your app's needs.
Finally, we wrap up by confirming our tests all pass and hint at the next episode, where we'll focus on permission checks and adding validation rules for extra security. Basically, by the end of this episode, you'll know exactly how to update user roles properly in your teams feature and feel confident it's working as intended!