On the land of website/software, there is always an Administrator. The Administrator, like a king, has the right to change the rules, block the news and ‘kill’ the innocent users whenever he likes.
Dieu et mon droit.
Today, I claimed to the throne – became an admin who had the power to delete comments and users.
rails g migration add_admin_to_users.
Second, create a table and run
Then, define a method in ApplicationController.
destroy to UsersController
Afterwards, add a delete link in the view.
Finally, we would like to destroy the posts and comments automatically at the same time we delete the users.
has_many :posts, dependent: :destroy
- Issue 1:
There would be a nil class error if there was no user signed in,
current_user.admin?went wrong. Therefore, I added another method in ApplicationController and change the line_to_be_modified to
- Issue 2:
Another error occurred when I tried to add the delete link on the comments partial.
At first, I passed in the ‘comment’ as $params, and it showed that the post.id and :id were nil. So I changed the $params to ‘comment.post.id, comment.id’ and it went well.