Ruby-on-rails 设计 - 创建用户帐户并确认而不发送电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7465467/
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
Devise - create user account with confirmed without sending out an email?
提问by disappearedng
I integrated devise with facebook. Now when I create a user account after the user has logged in with his/her facebook account,
我将设计与 facebook 集成。现在,当我在用户使用他/她的 Facebook 帐户登录后创建一个用户帐户时,
user = User.create(:email => data["email"],
:password => Devise.friendly_token[0,20])
user.confirmed_at = DateTime.now
user.save!
even though the account has been confirmed, an confirmation email is still fired. Any idea how I can turn the email firing off?
即使帐户已被确认,仍会发送确认电子邮件。知道如何关闭电子邮件触发吗?
回答by numbers1311407
The confirm callback happens after create, so it's happening on line 1 of your example, before you set confirmed_atmanually.
确认回调发生在创建之后,因此在您confirmed_at手动设置之前,它发生在示例的第 1 行。
As per the comments, the most correct thing to do would be to use the method provided for this purpose, #skip_confirmation!. Setting confirmed_atmanually will work, but it circumvents the provided API, which is something which should be avoided when possible.
根据评论,最正确的做法是使用为此目的提供的方法#skip_confirmation!。confirmed_at手动设置会起作用,但它绕过了提供的 API,这是应该尽可能避免的事情。
So, something like:
所以,像这样:
user = User.new(user_attrs)
user.skip_confirmation!
user.save!
Original answer:
原答案:
If you pass the confirmed_atalong with your createarguments, the mail should not be sent, as the test of whether or not an account is already "confirmed" is to look at whether or not that date is set.
如果您通过confirmed_at您的一起create论证,邮件不能发送,作为一个帐户是否是已测试“证实”是看是否该日期是集。
User.create(
:email => data['email'],
:password => Devise.friendly_token[0,20],
:confirmed_at => DateTime.now
)
That, or just use newinstead of createto build your user record.
那,或者只是使用new而不是create建立您的用户记录。
回答by Benjamin Crouzier
If you just want to prevent sending the email, you can use #skip_confirmation_notification, like so:
如果您只想阻止发送电子邮件,可以使用#skip_confirmation_notification,如下所示:
user = User.new(your, args)
user.skip_confirmation_notification!
user.save!
See documentation
查看文档
Skips sending the confirmation/reconfirmation notification email after_create/after_update. Unlike #skip_confirmation!, record still requires confirmation.
跳过发送确认/重新确认通知电子邮件 after_create/after_update。与#skip_confirmation! 不同,记录仍然需要确认。

