【问题标题】:Validation failed: Password can't be blank验证失败:密码不能为空
【发布时间】:2015-03-19 06:18:33
【问题描述】:

我有一个应用程序,基于 Rails 4.2 构建并尝试使用 RailsCasts #241 Simple OmniAuth 进行 Twitter 身份验证。
但是有这个问题:Validation failed: Password can't be blank!
我到处搜索答案,但没有找到解决方案!

user.rb

class User < ActiveRecord::Base

    has_secure_password
    validates :password, length: { minimum: 6 }, allow_blank: true
    def self.create_with_omniauth(auth)
        create! do |user|
            user.provider = auth.provider
            user.uid = auth.uid
            user.name = auth.info.name
        end
    end
end

sessions_controller.rb

class SessionsController < ApplicationController
   def login
   end

   def create
       @user = User.find_by_email(params[:email])
       if @user && @user.authenticate(params[:password])
           session[:user_id] = @user.id
           redirect_to root_path
       else
           redirect_to :back
       end
   end

   def create_session
       auth = request.env['omniauth.auth']
       user = User.find_by_provider_and_uid(auth['provider'], auth['uid'])    || User.create_with_omniauth(auth)
       session[:user_id] = user.id
       redirect_to root_url, notice: 'Signed in!'
   end

   def logout
       reset_session
       redirect_to root_path
   end
end

routes.rb

  get 'sessions/login'
  get 'sessions/logout'
  post 'sessions' => 'sessions#create'

  get '/auth/:provider/callback' => 'sessions#create_session'
  post '/auth/:provider/callback' => 'sessions#create_session'

  get 'registration' => 'users#new', as: 'registration'

解决方案
编辑 user.rb 模型后的样子:

class User < ActiveRecord::Base

    has_secure_password
    validates :password, length: { minimum: 6 }
    require 'securerandom'
    def self.create_with_omniauth(auth)
       create! do |user|
          user.provider = auth.provider
          user.uid = auth.uid
          user.name = auth.info.name
          user.password = SecureRandom.urlsafe_base64
       end
    end
end

【问题讨论】:

  • 尝试allow_blank 而不是allow_nil
  • 暂时放弃minimum验证,试试allow_blank
  • You need to supply at least one validation
  • 可能是 has_password 不接受空白的问题。顺便说一句:更新的方法是在没有密码的情况下让omniauth 写入身份表,然后将其与用户记录相关联。
  • 我该怎么做?

标签: ruby-on-rails omniauth ruby-on-rails-4.2 omniauth-twitter


【解决方案1】:

另一种方法是在创建用户时输入随机密码。例如omniauth-google-oauth2 自述文件中所示。所以你可以把你的代码改成这样:

require 'securerandom'
def self.create_with_omniauth(auth)
    create! do |user|
        user.provider = auth.provider
        user.uid = auth.uid
        user.name = auth.info.name
        user.password = SecureRandom.urlsafe_base64
    end
end

【讨论】:

  • 它似乎没有解决根本问题,下面的解决方案似乎不适用于我,但它现在正在工作!
【解决方案2】:

我觉得是

has_secure_password 

尝试评论它,你可以覆盖has_secure_password或者

has_secure_password(validations: false)

这是在自动添加时发生的

validates_presence_of :password_digest 

所以当指定 allow_blank 或 allow_null 时它不会起作用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 2015-04-19
    相关资源
    最近更新 更多