Created
August 9, 2010 17:45
-
-
Save CBedzz/515779 to your computer and use it in GitHub Desktop.
UsersController destroy method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The old version is the destroy method implemented in the UsersController | |
# for the admin to delete users. | |
# In the new version, I want to not allow the admin to delete himself. | |
# I was just wondering if there was a cleaner way to do it than the | |
# way I implemented it? | |
# OLD VERSION | |
def destroy | |
User.find(params[:id]).destroy | |
flash[:success] = "User destroyed." | |
redirect_to users_path | |
end | |
# NEW VERSION WITH "CAN'T DESTROY YOURSELF" CONSTRAINT | |
def destroy | |
user_to_destroy = User.find(params[:id]) | |
unless current_user?(user_to_destroy) | |
user_to_destroy.destroy | |
flash[:success] = "User destroyed." | |
else | |
flash[:error] = "Can't destroy yourself!" | |
end | |
redirect_to users_path | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment