如何在Devise中手动创建新用户和用户会话? [英] How to manually create a new user and user session in Devise?

查看:302
本文介绍了如何在Devise中手动创建新用户和用户会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,我在Rails中收集了大量信息。此表单的一部分是新用户注册的字段。由于Devise拥有专门用于创建新用户的控制器/操作,因此我不知道如何以完全不同的动作来编程创建另一个记录。我真的不能分开用户注册表。我不知道如何创建一个用户,然后记录用户,就像我可以轻松地在Authlogic中。



我已经使用了Authlogic和Devise,并认为每个人都有自己的优点和缺点。使用Devise,我喜欢用一个新项目走出去有多快,但是它的定制似乎是一种痛苦。 Authlogic在Rails 3上有很多问题,而我转而去Devise。我现在正在开发一个新项目,并从头开始。



所以我认为这个问题有两个潜在的答案:(a)如何做到这一点在Devise中,或者(b)为什么我应该用Rails 3切换到Authlogic。

解决方案

你可以创建一个新的Devise用户只需创建一个新的用户模型(请参阅 https://github.com/plataformatec/devise/wiki/How-To:-Manage-users-through-a-CRUD-interface

  @user = User.new(:email =>'test@example.com',:password =>'password',:password_confirmation =>'password' b $ b @ user.save 

要登录您新创建的用户,请使用 sign_in @user


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.

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.

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.

解决方案

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 => 'test@example.com', :password => 'password', :password_confirmation => 'password')
@user.save

To sign in your newly created user, use sign_in @user

这篇关于如何在Devise中手动创建新用户和用户会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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