在 Devise 中以管理员身份销毁用户 [英] Destroying users as an admin in Devise

查看:31
本文介绍了在 Devise 中以管理员身份销毁用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Devise 删除用户.我有一个用户列表,每个用户都有他们的电子邮件和他们旁边的删除"链接,只有我,管理员才能看到.我希望能够简单地单击删除链接以永久删除用户.以下代码删除我,管理员!

I am trying to use Devise to delete users. I have a list of users each with their email and a 'delete' link next to them, only visible to me, the administrator. I want to be able to simply click the delete link to remove the user forever. The following code deletes me, the administrator!

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

我认为您需要将要删除的用户的 :id 传递给某种destroy_user"方法:

I would think you need to pass the :id of the user you want to delete to some kind of 'destroy_user' method:

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

但是当您必须向 user_registration_path 提交 DELETE 请求时,您如何执行此操作??

But how do you do this when you have to submit a DELETE request to the user_registration_path??

------ 编辑--------

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

好的,我已将此方法添加到我的用户控制器中:

OK, I've added this method to my users controller:

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

因此,我需要告诉用户控制器在收到 DELETE 请求时调用 destroy 方法.你如何在routes.rb中做到这一点?目前我有:

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

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

我需要类似的东西:

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

但这不起作用.链接中应该包含什么?:

but this doesn't work. And what should go in the link?:

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

换句话说,你应该在 routes.rb 文件中放什么来区分对同一个 url 的不同请求类型(GET、DELETE 等)?

To put it another way, what should you put in the routes.rb file in order to distinguish between different request types (GET, DELETE etc) to the same url?

推荐答案

Devise 不提供删除其他用户的操作,仅删除当前登录的用户.您必须在其中一个控制器中创建自己的操作(最有可能的是,无论哪个控制器具有显示所有用户的操作)来处理删除当前登录用户以外的用户.

Devise doesn't provide an action to delete another user, only to delete the currently logged in user. You'd have to create your own action in one of your controllers (most likely, whichever controller has the action to display all the users) to handle deleting a user other than the currently logged in one.

这篇关于在 Devise 中以管理员身份销毁用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆