Devise skip_confirmation! fails to avoid to send the confirmation instructions

冷暖自知 提交于 2019-11-30 13:17:57

问题


My app is set up so that if a user signs in with Oauth or Openid, they don't have to confirm their email address. However, Devise is still sending email confirmations. When I call User.skip_confirmation! I get an undefined method error. My model:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, 
  :trackable, :validatable, :confirmable, :lockable, :token_authenticatable, :omniauthable

  attr_accessible :username, :email, :password, :password_confirmation, :remember_me
  validates_presence_of :username
  validates_uniqueness_of :username, :case_sensitive => false

  def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
    data = access_token.extra.raw_info
    if user = User.where(:email => data.email).first
      user
    else 
      #User.skip_confirmation!
      User.create!(:username => data.name, :email => data.email, :password =>    Devise.friendly_token[0,20]) 
    end
  end

  def skip_confirmation!
   self.confirmed_at = Time.now
 end
end

My Controller:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
    @user.skip_confirmation!
    if @user.persisted?
      sign_in @user
      @fname = @user.username
      redirect_to root_path, :flash => { :success => "Welcome #{@fname}!" }
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end

thanks for any help.


回答1:


You need to skip confirmation before you create the User objects and its persisted to the database. So the user creation part of your method would look like

user = User.new(:username => data.name, :email => data.email, :password =>    Devise.friendly_token[0,20])
user.skip_confirmation!
user.save



回答2:


If you're updating a user record, make sure to use skip_reconfirmation! (mind the re)



来源:https://stackoverflow.com/questions/8672189/devise-skip-confirmation-fails-to-avoid-to-send-the-confirmation-instructions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!