使用 Devise 进行管理员用户管理
我是第一次尝试 Devise。我想做的事情之一是为管理员用户提供一个界面来创建、查找和编辑用户。这就是我可能出错的地方。
我创建了一个 PeopleController 类,它继承自 ApplicationController,列出人员并提供用于创建和更新用户的方法和视图。除了一个例外,一切都工作正常。当管理员用户更新自己的记录时,会话将被清除,他们必须在保存后重新登录。
在此应用程序中,我没有使用可注册模块。只有管理员用户才能创建新用户。设计提供用户管理工具的正确方法是什么?创建我自己的控制器似乎是一条错误的道路。
预先感谢您的帮助。
I am trying out Devise for the first time. One of the things that I wanted to do is provide an interface for Admin users to create, find and edit users. Here's where I may have gone wrong.
I created a PeopleController class which inherits from ApplicationController that lists people and provides methods and views for creating and updating users. Everything works fine with one exception. When the admin user updates their own record, the session is cleared and they have to login again after saving it.
In this application I'm not using the registerable module. Only an admin user can create new users. What is the right way in devise to provide user management tools. Creating my own controller seems to have been the wrong path to take.
Thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
非常感谢您的帮助。这基本上正是我正在做的事情。我发现了一个线索,帮助我解决了用户在这个wiki中编辑自己的记录时会话被清除的问题:
https://github.com /plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password
这是我需要的行:
此方法位于Devise::Controllers::Helpers 所以我在我的控制器中做了这个。
然后在我的更新方法中,仅当 current_user.id 等于正在编辑的 id 时才调用它:
现在,如果当前用户编辑自己的记录,会话将在保存后恢复。
再次感谢您的回复。
Thank you very much for the help. This is essentially exactly what I am doing. I discovered a clue that helped me solve the problem of the user's session being cleared when they edit their own record in this wiki:
https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password
This is the line I needed:
This method is located in Devise::Controllers::Helpers so I did this in my controller.
Then in my update method I call it only if the current_user.id equals the id that is being edited:
Now if the current user edits their own record, the session is restored after it is saved.
Thanks again for your responses.
这就是我在我的一个应用程序中管理用户的方式。我只生成了一个
User
类,我通过此迁移添加了一个
role
列:以及我的
User
模型:然后创建新用户您所要做的就是在控制器(甚至可能是子类
Devise::RegistrationsController
)中提供一个自定义方法,如下所示:This is how I manage users in one of my apps. I have only one
User
class generated withto which I added a
role
column with this migration:and my
User
model:Then to create new users all you would have to do is provide a custom method in a controller (maybe even subclass
Devise::RegistrationsController
) like this: