Killing users as an administrator in Devise

I am trying to use Devise to delete users. I have a list of users, each of whom has his own e-mail and a delete link next to them, only for me, the administrator. I want to be able to simply click the delete link to delete the user permanently. The following code removes me, admin!

<%= link_to "delete", user_registration_path, :method => :delete, :confirm => "You sure?" %> 

I think you need to pass the identifier of the user you want to delete to some destroy_user method:

 @user.find(params[:id]).destroy_user 

But how do you do this when you need to send a DELETE request to user_registration_path?

------ EDIT --------

OK, I added this method to my user controller:

 def destroy User.find(params[:id]).destroy flash[:success] = "User destroyed." redirect_to users_path end 

So, I need to tell the user controller to call the destroy method when it receives a DELETE request. How do you do this in routes.rb? I currently have:

 match '/users/:id', :to => 'users#show', :as => :user match '/all_users', :to => 'users#index', :as => :all_users 

I need something like:

 match 'delete_user', :to => 'users#destroy', :as => :destroy_user, :method => :delete 

but it does not work. And what should follow the link ?:

 <%= link_to "delete", destroy_user, :method => :delete, :confirm => "You sure?" %> 

In other words, what should you put in the route.rb file to distinguish between different types of requests (GET, DELETE, etc.) with the same URL?

+4
source share
3 answers

Devise does not provide an action to delete another user, only to delete the current user. You will need to create your own action in one of your controllers (most likely, whichever controller has an action to display all users) in order to handle the removal of a user other than the current one registered in it.

+2
source

Replace the "user" with the actual user you want to destroy, for example: if you print the email as user.email, then the user plugin there and howled

 <%= link_to "delete", user_registration_path(user), :method => :delete, :confirm => "You sure?" %> 
+4
source

Got! Just need to add: via the argument in the routes:

 match '/users/:id', :to => 'users#show', :as => :user, :via => :get match '/users/:id', :to => 'users#destroy', :as => :destroy_user, :via => :delete 
0
source

Source: https://habr.com/ru/post/1342022/


All Articles