Devise:允许管理员编辑其他用户 - Rails
我试图允许管理员用户在 Devise 中编辑其他用户,但是当我尝试访问另一个用户的编辑页面(例如 /users/1/edit)时,我收到以下消息:
Unknown action
Could not find devise mapping for path "/users/1/edit"
唯一的路径似乎work是/users/edit,显示当前用户的编辑页面。
在我的路线文件中,我有:
devise_for :users, :controllers => { :registrations => 'users' }
resources :users
有什么想法吗?谢谢!
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 (e.g. /users/1/edit), I get the following message:
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.
In my routes file I have:
devise_for :users, :controllers => { :registrations => 'users' }
resources :users
Any ideas? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也必须这样做,而且它目前还没有内置到设备中。由于投票最多的答案有一个死链接,我想我应该在这里发布我的解决方案。
您需要创建一个 UsersController 并自行构建表单和控制器,但是您还需要隔离设计设置的 UsersController。为此,请在
routes.rb
文件中将devise_for :users
调用修改为这会将所有默认的 devise-handled 路由更改为 /d/users/..并让您拥有 /users/... 路径,让您以管理员身份管理用户。
Devise 还在其 wiki 中解决了这个问题。
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. To do this, in your
routes.rb
file, modify yourdevise_for :users
call toThis 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.
Devise also addresses this in their wiki.
Devise 非常适合用户身份验证,但它不内置对管理用户的支持。所以你必须自己构建它。
这是 如何执行此操作的示例。这个例子已经有几个月了,但它应该为您指明正确的方向。
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.
我已经完成了你想做的事情,而且你的路线看起来是正确的。
您还需要创建一个 UsersController 来处理您想要对用户执行的所有 CRUD 操作。这与 Devise 是分开的。
当您的 UsersController 存在时,如果 current_user 不是管理员,则只能通过重定向(可能在 before 过滤器中)允许管理员用户访问特定操作。
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. This is separate from 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.