2016-11-09 19 views
0

我需要將跳過驗證方法添加到會話控制器,但我不確定將omniauth as和基本登錄名定義爲什麼。即它們都是從類的用戶,但在會話控制器做我把在rails中跳過驗證,如何定義類的每個實例

def create 
    user = User.from_omniauth(env["omniauth.auth"]) 
**user.from_omniauth.skip_name_validation = true** 
    etc etc 

然後也希望在模型中的方法添加驗證爲把粗體,並添加到我的會話控制器每次登錄,這樣兩個實例能夠被繞過。

我的會話控制器

def create 
    user = User.from_omniauth(env["omniauth.auth"]) 

    unless user.present? 
    user = User.find_by(email: params[:session][:email].downcase) 
    if user && user.authenticate(params[:session][:password]) 
     log_in user 
     redirect_to user 
     # Log the user in and redirect to the user's show page. 
    else 
     # Create an error message. 
     flash.now[:danger] = 'Invalid email/password combination' 
     render 'new' 
    end  
    else   
    log_in user 
    redirect_to user 
    end 
end 

我model.rb

class User < ApplicationRecord 

    before_save { self.email = email.downcase } 
    validates :name, presence: true, length: { maximum: 50 }, 
**unless: :skip_name_validation** 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    validates :email, presence: true, length: { maximum: 255 }, 
        format: { with: VALID_EMAIL_REGEX }, 
        uniqueness: { case_sensitive: false }, 
        **unless: :skip_email_validation** 
    has_secure_password 
    validates :password, presence: true, length: { minimum: 6 }, allow_nil:true, 
**unless: :skip_password_validation** 

**attr_accessor :skip_name_validation, :skip_email_validation, :skip_password_validation** 

**attr_accessor :skip_User, :skip_self** 

**validate :User, unless: :skip_User** 
**validate :self, unless: :skip_self** 

    def User.digest(string) 
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : 
                BCrypt::Engine.cost 
    BCrypt::Password.create(string, cost: cost) 
end 

    def self.from_omniauth(auth) 
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 
     user.provider = auth.provider 
     user.uid = auth.uid 
     user.name = auth.info.name 
     user.oauth_token = auth.credentials.token 
     user.oauth_expires_at = Time.at(auth.credentials.expires_at) 
     user.save! 
    end 
    end 
end 
+0

是由我提供的信息對您有所幫助? –

+0

有幫助,但當我疲倦的時候不是很正確。 –

+0

有人,對此有什麼想法? –

回答

0
class User < ApplicationRecord 
     attr_accessor :password_confirmation, :skip_password_validation 
     validates :password, :presence => true ,unless: :skip_password_validation 
    validates_confirmation_of :password, :if => ->{ password.present? },unless: :skip_password_validation 

     def User.digest(string) 
     cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : 
                 BCrypt::Engine.cost 
     BCrypt::Password.create(string, cost: cost) 
    end 

     def self.from_omniauth(auth) 
     where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 
      user.provider = auth.provider 
      user.uid = auth.uid 
      user.name = auth.info.name 
      user.oauth_token = auth.credentials.token 
      user.oauth_expires_at = Time.at(auth.credentials.expires_at) 
      user.save! 
     end 
     end 
    end 


    def create 
     user = User.from_omniauth(env["omniauth.auth"]) 
     #######try this ##### 
    user.skip_password_validation = true 
####or try this #### 
    user.from_omniauth.skip_name_validation = true 

     unless user.present? 
     user = User.find_by(email: params[:session][:email].downcase) 
     if user && user.authenticate(params[:session][:password]) 
      log_in user 
      redirect_to user 
      # Log the user in and redirect to the user's show page. 
     else 
      # Create an error message. 
      flash.now[:danger] = 'Invalid email/password combination' 
      render 'new' 
     end  
     else   
     log_in user 
     redirect_to user 
     end 
    end 
+0

該代碼仍然說電子郵件不能爲空,密碼不能爲空密碼不能空白應至少被淘汰 –

相關問題