In this episode, we're taking what we did in the previous video with deleting individual posts and applying it to entire discussions. The big difference is that when you delete a discussion, we want all the posts inside that discussion to be removed too, which our database will handle for us. To get started, we create a new discussion (creatively titled "Delete Me") so we have something to test the deletion on.
We then dig into authorization, making sure that only the owner of the discussion can delete it. We update our policy to check if the current user is the one who created the discussion before allowing them to delete.
Next, we head to the UI and add a "Delete" button next to the discussion title (but only show it for discussions you own). We use standard styling and make sure it's hidden for users who don't have permission.
Then it's on to wiring things up: we add the click handler to call a deleteDiscussion
method, set up a controller and route for deleting a discussion, and create the request class that handles authorization and deletion logic on the backend. After deleting, we redirect the user back to the home page since the discussion is now gone.
To finish up, we fix a little bug where we forgot to hook up the button click, and add a JavaScript confirmation popup to prevent accidental deletions. At the end of the video, we successfully delete the test discussion and confirm everything works as expected!