我試圖跳過密碼驗證,因爲Facebook沒有返回登錄密碼。在試圖跳過驗證的rails中使用attr_accessor
我收到錯誤;
「驗證失敗:密碼不能爲空」上線user.save!
應用跟蹤;
app/models/user.rb:36:in block in 'from_omniauth'
app/models/user.rb:29:in 'from_omniauth'
app/controllers/sessions_controller.rb:6:in 'create'
這是因爲from_omniauth
是一個類的方法,包裹用戶變量裏面,然後我試圖跳過密碼驗證,因爲,一個實例變量,當尚未創建的實例。即first_or_create do |user|
尚未在實例之前創建或註冊用戶?
如果是這樣,我想知道如何重構我的代碼,使其工作?
會話控制器
class SessionsController < ApplicationController
def create
user = User.from_omniauth(env["omniauth.auth"])
user.skip_password_validation = true
...
model.rb
class User < ApplicationRecord
has_secure_password
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true,
unless: :skip_password_validation
attr_accessor :skip_password_validation
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.email = auth.info.email
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
end
奇怪的是我仍然得到相同的錯誤。 –
您的數據庫中設置了密碼嗎? –
yes,schema是't.string'password_digest'' –