简体   繁体   English

设计:允许管理员编辑其他用户 - Rails

[英]Devise: Allow admins to edit other users - Rails

I'm trying to allow an admin user to edit other users in Devise, but when I try to access an edit page for another user (eg /users/1/edit), I get the following message: 我正在尝试允许管理员用户在Devise中编辑其他用户,但是当我尝试访问另一个用户的编辑页面时(例如/ users / 1 / edit),我收到以下消息:

Unknown action

Could not find devise mapping for path "/users/1/edit"

The only path that seems to work is /users/edit, which shows the edit page for the current user. 似乎有效的唯一路径是/ users / edit,它显示当前用户的编辑页面。

In my routes file I have: 在我的路线文件中,我有:

devise_for :users, :controllers  => { :registrations => 'users' }
resources :users

Any ideas? 有任何想法吗? Thanks! 谢谢!

I've had to do this as well, and it's not currently built into devise. 我也必须这样做,而且它目前还不是设计的。 Since the answer most upvoted has a dead link, I thought I'd post my solution here. 由于最受欢迎的答案有一个死链接,我以为我会在这里发布我的解决方案。

You need to create a UsersController and built out your forms and controller on your own, but then you also need to isolate the UsersController that devise sets up. 您需要创建一个UsersController并自己构建表单和控制器,但是您还需要隔离设计设置的UsersController。 To do this, in your routes.rb file, modify your devise_for :users call to 为此,请在routes.rb文件中修改您的devise_for :users调用

devise_for :users, :path_prefix => 'd' # routes for devise modules on User

resources :users # custom admin-type CRUD for users

This will change all your default devise-handled routes to /d/users/... and let you have the /users/... path to let you manage users as an admin. 这会将所有默认设计处理的路由更改为/ d / users / ...并让您拥有/ users / ...路径,以便您以管理员身份管理用户。

Devise also addresses this in their wiki . Devise也在他们的wiki中解决了这个问题

Devise is great for user authentication but it does not come with built in support for managing users. 设计是伟大的用户身份验证 ,但它具有内置的用于管理用户的支持。 So you'll have to build that yourself. 所以你必须自己构建它。

Here's an example of how to do it. 这是一个如何做到这一点的例子 The example is a few months old but it should point you in the right direction. 这个例子有几个月了,但它应该指向正确的方向。

I've done what you're trying to do, and your routes look right. 我已经做了你想要做的事情,你的路线看起来正确。

You need to also create a UsersController that handles all of the CRUD actions you want to perform on users. 您还需要创建一个UsersController来处理您要对用户执行的所有CRUD操作。 This is separate from Devise. 这与Devise是分开的。

When your UsersController is there, you can only allow admin users access to particular actions by redirecting (perhaps in a before filter) if the current_user is not an admin. 当您的UsersController存在时,如果current_user不是管理员,则只允许管理员用户通过重定向(可能在之前的过滤器中)来访问特定操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM