Skip to content

Instantly share code, notes, and snippets.

@CBedzz
Created August 9, 2010 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CBedzz/515779 to your computer and use it in GitHub Desktop.
Save CBedzz/515779 to your computer and use it in GitHub Desktop.
UsersController destroy method
# 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