问题描述
我有一个表格,可以在 Rails 中收集大量信息.此表单的一部分是供新用户注册的字段.由于 Devise 具有专门用于创建新用户的控制器/操作,我不知道如何以编程方式在完全不同的操作中创建用户,同时还创建另一条记录.我真的不能将用户注册表分开.我不知道如何创建用户,然后让用户登录,就像我在 Authlogic 中可以轻松完成的那样.
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.
我使用过 Authlogic 和 Devise,并认为每个都有自己的优点和缺点.使用 Devise,我喜欢开始"一个新项目的速度有多快,但定制它似乎很痛苦.Authlogic 不久前在 Rails 3 上遇到了很多问题,所以我改用了 Devise.我现在正在着手一个新项目,需要从头开始.
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.
所以我认为这个问题有两个可能的答案:(a) 如何在 Devise 中做到这一点,或者 (b) 为什么我应该改为使用 Rails 3 切换到 Authlogic.
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.
推荐答案
你可以通过创建一个新的用户模型来创建一个新的 Devise 用户(参见 https://github.com/plataformatec/devise/wiki/How-To:-Manage-users-through-a-CRUD 接口)
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)
@user = User.new(:email => '[email protected]', :password => 'password', :password_confirmation => 'password')
@user.save
要登录新创建的用户,请使用 sign_in @user
To sign in your newly created user, use sign_in @user
这篇关于如何在 Devise 中手动创建新用户和用户会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!