Ruby-on-rails 如何在 Devise 中手动创建新用户和用户会话?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4660565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to manually create a new user and user session in Devise?
提问by Sean Coleman
I have a form where I collect a lot of information in Rails. Part of this form is fields for a new user to register. Since Devise has controllers/actions specifically to create a new user, I don't know how to programmatically create a user in an entirely different action that also creates another record. I really can't have the user registration form separate. I can't figure out how to create a user, and then log the user in, like I could easily do in Authlogic.
我有一个表格,可以在 Rails 中收集大量信息。此表单的一部分是供新用户注册的字段。由于 Devise 具有专门用于创建新用户的控制器/操作,我不知道如何以编程方式在完全不同的操作中创建用户,同时还创建另一条记录。我真的不能将用户注册表单分开。我不知道如何创建用户,然后让用户登录,就像我在 Authlogic 中可以轻松做到的那样。
I have used both Authlogic and Devise, and think each has their strengths and weaknesses. With Devise, I love how quick it is to "get going" with a new project, but customizing it seems to be a pain. Authlogic had so many problems with Rails 3 a while back, that I switched to Devise. I'm now working on a new project and get to start from scratch.
我使用过 Authlogic 和 Devise,并认为每个都有自己的优点和缺点。使用 Devise,我喜欢“开始”一个新项目的速度有多快,但定制它似乎很痛苦。Authlogic 不久前在 Rails 3 上遇到了很多问题,所以我改用了 Devise。我现在正在从事一个新项目,并从头开始。
So I think there are 2 potential answers to this question: (a) how to do this in Devise, or (b) why I should just switch to Authlogic with Rails 3 instead.
所以我认为这个问题有两个可能的答案:(a) 如何在 Devise 中做到这一点,或者 (b) 为什么我应该改用 Rails 3 切换到 Authlogic。
回答by David Sulc
You can create a new Devise user simply by creating a new user model (see https://github.com/plataformatec/devise/wiki/How-To:-Manage-users-through-a-CRUD-interface)
您只需创建一个新的用户模型即可创建一个新的 Devise 用户(请参阅https://github.com/plataformatec/devise/wiki/How-To:-Manage-users-through-a-CRUD-interface)
@user = User.new(:email => '[email protected]', :password => 'password', :password_confirmation => 'password')
@user.save
To sign in your newly created user, use sign_in @user
要登录新创建的用户,请使用 sign_in @user
回答by vanship82
Saving the newly created user before sign_in will fail because devise has not yet filled the required fields for User object yet. So David's code will work for the current page, but the next page won't get the signed in user, since the user is only saved after the session is set. This happens when I use Mongoid, and I don't know if it is a problem specific to Mongodb.
在 sign_in 之前保存新创建的用户将失败,因为 devise 尚未填充 User 对象的必填字段。因此,David 的代码将适用于当前页面,但下一页将不会获得已登录用户,因为该用户仅在设置会话后才保存。我在使用Mongoid时出现这种情况,不知道是不是Mongodb特有的问题。
To address this problem, I have a very imperfect solution to call sign_in twice. The first sign_in will save the user. It just works but people can improve it certainly.
为了解决这个问题,我有一个非常不完美的解决方案来调用 sign_in 两次。第一次 sign_in 将保存用户。它只是有效,但人们当然可以改进它。
@user = User.new(:email => '[email protected]',
:password => 'password',
:password_confirmation => 'password')
# This will save the user in db with fields for devise
sign_in @user
# :bypass is set to ignore devise related callbacks and only save the
# user into session.
sign_in @user, :bypass => true
回答by Raúl Contreras
For new people seeing this question...
对于看到这个问题的新人......
A simple way to do this is in your
一个简单的方法是在你的
config/routes.rb
you should have a line like the following :
你应该有如下一行:
devise_for :users
so, you just have to add a path prefix that devise will use:
所以,你只需要添加一个设计将使用的路径前缀:
devise_for :users, :path_prefix =>'auth'
Hope it helps!
希望能帮助到你!

