ensure last admin remains while destroying/deleting/removing an user in devise authentication
I'm new to rails.I'm working on a small application which needs an admin role who can add, modify and delete the content. So some models/views should be hidden from general users and only admin can have access to them through authentication.
To achieve this I have used 'rails_admin' and 'devise' gems. I did not the want the sign_up page to be shown to genral user, so I have removed that removing :registerable from my Admin model.
Now everything seems fine but, when I log in through an admin, delete each and every admin from my dashboard, I cannot login again since I have removed the sign_up page.
So I want to ensure that my last user remains. A warning/notice should be shown user while deleting the last user. I wrote the following code for that
private def ensure_an_admin_remains if Admin.count.zero? raise "Can't delete last user" end end
This throws an error but my browser shows all the Error details. I want it to be shown to as a flash message. I think I have to change the devise controllers destroy method. But I don't know where to find it and how to do the required changes.
Can someone please help me on this?
def destroy admin = Admin.find(params[:id]) begin admin.destroy redirect_to admins_path, notice: 'Admin destroyed.' rescue => e redirect_to admins_path, alert: e.message end end