Ruby On Rails Lesson
The destroy Action
The final step needed to complete the Users resource is to add delete links and a destroy action. We’ll start by adding a delete link for each user on the user index page, restricting access to administrative users.
To write tests for the delete functionality, it’s helpful to be able to have a factory to create admins. We can accomplish this by adding an :admin block to our factories, as shown in Listing 9.43.
With the code in Listing 9.43, we can now use FactoryGirl.create(:admin) to create an administrative user in our tests.
Our security model requires that ordinary users not see delete links:
But administrative users should see such links, and by clicking on a delete link we expect an admin to delete the user, i.e., to change the User count by -1: