我需要一个“用户"吗?在 Rails 中使用 Devise 时的控制器 [英] Do I need a "Users" controller when using Devise in Rails

查看:10
本文介绍了我需要一个“用户"吗?在 Rails 中使用 Devise 时的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Rails 新手.我正在从事一个小型 Rails4 项目,试图提高我的技能.我松散地关注 M.Hartl 的教程.

I am a Rails newbie. I am working on a small Rails4 project trying to improve my skills. I am loosely following M.Hartl's tutorial.

根据教程构建了自定义用户身份验证.但是我想使用 Devise 进行用户身份验证.我还需要像教程中那样使用用户控制器吗?在已经使用 Devise 的情况下,我应该在哪些情况下使用/不使用用户控制器?

As per the tutorial a custom user authentication is built. However I would like to use Devise for User authentication. Do I still need to have a Users controller as in the tutorial? In which cases should I use/not use a Users controller when already using Devise?

关于教程,我是跳过控制器生成部分还是必须将动作映射到设计?

Concerning the tutorial, do I just skip the controller generating part or do I have to map the actions to Devise?

推荐答案

如果您想与正常的注册/更新过程分开管理用户,您只需要一个用户控制器.我有一个用户控制器,以便管理员可以独立于正常的设备注册/更新过程来管理(创建、编辑、更新、删除)用户.

You only need a users controller if you want to manage users separately from the normal signup/update process. I have a users controller so that admins can manage (create, edit, update, delete) users independently of the normal devise signup/update process.

与 devise 的冲突可能是因为你的路由文件中有 devise_for :users ... 来设置 devise,并且你的 UsersController 也有 resources :users.这意味着 devise 和您的 UsersController 将尝试共享一些相同的 /users 路由.您需要通过更改其中之一映射到的路径来将它们分开.例如,您可以添加 :path =>;'u' 到您的 devise_for 语句,以便设计路由映射到 /u 并且不会与 /users.或者,您可以单独保留 devise_for(因此仍然使用 /users),而是将您的 UsersController 路由更改为例如 resources :users_admin, :controller =>'users' 它将移动您的 UsersControllers 路由以映射到 /users_admin.请注意,这会将路径助手从例如 users_path 更改为 users_admin_path.

The conflict with devise is probably because you have devise_for :users … in your routes file to set up devise and also have resources :users for your UsersController. This means that devise and your UsersController will be trying to share some of the same /users routes. You need to separate them out by changing the path that one of them is mapped to. You could either add, for example, :path => 'u' to your devise_for statement so that devise routes are mapped to /u and won't conflict with your UsersController on /users. Alternatively you could leave the devise_for alone (therefore still using /users) and instead change your UsersController routing to, for example, resources :users_admin, :controller => 'users' which would move your UsersControllers routes to be mapped to /users_admin. Note that this would change the path helpers from, for example, users_path to users_admin_path.

更新

根据您的评论/编辑,我快速浏览了教程,我认为该设计基本上为您提供了与用户相关功能的等效功能,该功能从第 5.4 节发展到大约第 9.1 或 9.2 节.(加上一些额外的东西,例如,电子邮件确认、密码重置、帐户锁定等).但是,如果您想尝试将 Devise 与该教程合并,这并不意味着它是该功能的直接替代品.有些东西看起来可以工作(例如,Devise 还定义了一个 current_user 方法),但是路由等会有所不同,并且 devise 将事物拆分为更多控制器(用于注册的单独控制器,登录/退出,密码重置...).管理员类型的功能(如第 2.2、9.3、9.4 节 - 创建/编辑/删除/列表 other 用户)是我在我的应用程序中添加的单独的 UsersController.Devise 没有定义一个 UsersController,但是如果你在没有我上面提到的路径的情况下执行 devise_for :users ,它会使用 users 路由.

Following your comment/edit, I've had a quick look at the tutorial and I think that devise basically gives you the equivalent functionality of the user-related functionality which is developed from section 5.4 to about sections 9.1 or 9.2. (plus some extra stuff, for example, email confirmation, password reset, account lockout etc.). However, that doesn't mean that it's a drop-in replacement for that functionality, if you want to try and merge Devise with that tutorial. There are some things that look like they would work (e.g. Devise also defines a current_user method), but the routes etc. would be different, and devise splits things up into more controllers (separate controllers for registration, sign in/out, password reset…). The admin-type functionality (like in sections 2.2, 9.3, 9.4 - create/edit/delete/list other users) is what I've added a separate UsersController for in my app. Devise doesn't define a UsersController, but does use the users routes if you do devise_for :users without a path as I mentioned above.

所以,更具体地说:

  1. 如果您想启用允许查看/编辑/删除所有用户的管理员类型功能,您只需要一个 UsersController.
  2. 如果您想在教程中使用设计,则可能需要做一些工作来调整内容以使其适合,更改页面上的帮助链接等.抱歉,我不是更具体;我还没有完成那个教程.

您可能会错过自己手动完成所有工作所带来的额外理解,但 devise 是一个流行的引擎,因此了解它也很好.如果你有时间,你可以完全完成教程,然后再用设计!这将帮助您了解设计在幕后所做的一些事情.P.S:查看设计源代码会很有启发性,即使您不能立即完全理解.

You would be missing out on the extra understanding that comes from doing it all manually yourself, but devise is a popular engine, so it's good to know as well. If you have the time, you could do the tutorial entirely, and then again with devise! It would help you understand some of the kind of stuff devise is doing behind the scenes. P.S: It can be instructive to look at the devise source code, even if you don't understand it all immediately.

这篇关于我需要一个“用户"吗?在 Rails 中使用 Devise 时的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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