Rails 3 with Devise for Authentication - 如何手动创建用户? [英] Rails 3 with Devise for Authentication - How do I manually create a user?
问题描述
我想手动创建新的用户
,而不是强迫他们验证他们的电子邮件地址.
I would like to manually create new User
s, without forcing them to verify their email address.
这个想法是让现有用户无需注册即可自动添加他们的朋友.这对我正在努力解决的业务案例很有意义.
The idea is to allow existing users to automatically add their friends without requiring their registration. It makes sense for the business case I'm working to solve.
Devise 如何实现这一目标?
How can this be achieved with Devise?
推荐答案
skip_confirmation!
方法可用于任何 confirmable
模型.
The skip_confirmation!
method is available to any confirmable
model.
@user = User.new params[:user]
@user.skip_confirmation! # Sets confirmed_at to Time.now, activating the account
@user.save
用户帐户将被激活.如果您不希望那样,请继续阅读.
The user account will be activated though. If you don't want that, continue reading.
Devise 使用条件回调来生成确认令牌并发送电子邮件.仅当 confirmation_required?
返回 true
时才会调用回调.在您的模型上重新定义它:
Devise uses conditional callbacks to generate the confirmation token and send the email. The callbacks will be called only if confirmation_required?
returns true
. Redefine it on your model:
def confirmation_required?
false
end
但是,这会使 active_for_authentication?
方法始终返回 true,因为它会考虑是否需要确认.我们也必须重新定义它:
However, this will make the active_for_authentication?
method always return true because it takes whether or not confirmation is required into account. We have to redefine that as well:
def active_for_authentication?
confirmed? || confirmation_period_valid?
end
这样,帐户将保持不活动状态,并且不会发送确认电子邮件.您必须通过调用记录中的 confirm!
或将 confirmed_at
设置为任何日期来手动激活用户.
This way, the account will stay inactive and no confirmation email will be sent. You will have to manually activate the user by calling confirm!
on the record or just setting confirmed_at
to any date.
这是一个相当大的黑客,但它应该可以工作.
It's quite a hack, but it should work.
这篇关于Rails 3 with Devise for Authentication - 如何手动创建用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!