2016-11-09 3 views
0

私のセッションコントローラにスキップ検証メソッドを追加する必要がありますが、omniauthを基本ログインとしてどのように定義するかはわかりません。すなわち、それらはクラスのユーザーからではなく、セッションコントローラの両方であり、私はまた、各ログインのように両方のインスタンスを太字に入れて、モデル内のメソッドの検証を追加して、私のセッションコントローラにそれを追加したいその後レールのバリデーションをスキップする、クラスの各インスタンスを定義する方法

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

そのコードはまだ電子メールが空白ではないと言っています、パスワードは空白にできません。パスワードは空白にしてください。 –

関連する問題