且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我需要一个“用户”控制器在使用Devise in Rails时

更新时间:2022-10-20 20:40:14

如果要从普通用户分开管理用户,只需要一个用户控制器注册/更新过程。我有一个用户控制器,以便管理员可以独立于正常的设计注册/更新过程来管理(创建,编辑,更新和删除)用户。



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



更新



在您的评论/编辑之后,我已经快速浏览了本教程,我认为该设计基本上为您提供了从5.4节到9.1节或9.2节开发的用户相关功能的等效功能。 (加上一些额外的东西,例如电子邮件确认,密码重置,帐户锁定等)。但是,如果您希望尝试并将Devise与该教程合并,那么这并不意味着它将成为该功能的替代品。有些东西看起来像他们会工作(例如,Devise还定义了一个 current_user 方法),但是路线等将是不同的,并且设计成更多的控制器(单独的控制器注册,登录/注销,密码重置...)。管理员功能(如2.2,9.3,9.4 - 创建/编辑/删除/列出其他用户)是我在应用程序中添加了一个单独的UsersController。 Devise没有定义一个UsersController,但是如果您使用用户路由,则不使用路径作为 devise_for:users



所以更具体地说:


  1. 你只需要如果要启用管理员类型功能,允许查看/编辑/删除所有用户。

  2. 如果您想在教程中使用devise,那么可能需要一些按摩事情适应,改变页面上的帮助链接等对不起我不是更具体;我没有完成这个教程。

你会错过从手动自己手动完成的额外的理解,但是设计是一个受欢迎的引擎,所以很高兴知道。如果你有时间,你可以完全完成本教程,然后再用设计!这将有助于您了解一些设计在幕后的东西。 P.S:查看设计源代码是有启发意义的,即使你不明白这一切。


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.

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.

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.

UPDATE

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.

So, more specifically:

  1. You would only need a UsersController if you want to enable admin-type functionality allowing viewing/editing/deleting all users.
  2. If you wanted to use devise in the tutorial, it would probably need some work to massage things to fit, changing helper links on pages etc. Sorry I'm not more specific; I haven't done that tutorial.

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.