开发者

Devise - create user account with confirmed without sending out an email?

开发者 https://www.devze.com 2023-04-05 23:07 出处:网络
I integrated 开发者_运维知识库devise with facebook. Now when I create a user account after the user has logged in with his/her facebook account,

I integrated 开发者_运维知识库devise with facebook. Now when I create a user account after the user has logged in with his/her facebook account,

  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?


The confirm callback happens after create, so it's happening on line 1 of your example, before you set confirmed_at manually.

As per the comments, the most correct thing to do would be to use the method provided for this purpose, #skip_confirmation!. Setting confirmed_at manually will work, but it circumvents the provided API, which is something which should be avoided when possible.

So, something like:

user = User.new(user_attrs)
user.skip_confirmation!
user.save!

Original answer:

If you pass the confirmed_at along with your create arguments, 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.

User.create(
  :email => data['email'], 
  :password => Devise.friendly_token[0,20], 
  :confirmed_at => DateTime.now
)

That, or just use new instead of create to build your user record.


If you just want to prevent sending the email, you can use #skip_confirmation_notification, like so:

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.


Open up the Rails console

rails c

Note the user (through id) or using rails helper methods, eg. first, last. Create a variable to hold the user.

user =  User.last

Use the skip_confirmation helper to confirm the user, then save.

user.skip_confirmation
user.save
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号