设计用户不保存密码(在控制台中)

时间:2015-10-01 13:16:50

标签: ruby-on-rails devise

解决方法

在worker.rb中:

   before_save: set_defaults    
   private
    def set_defaults
      if self.encrypted_password.blank?
        self.encrypted_password = Worker.new.send(:password_digest, password)
      end
      self.has_accepted_terms = false if has_accepted_terms.nil?
      self.live = false if live.nil?
      self.email.downcase!
      self.first_name ||= ""
      self.last_name ||= ""
      self.phone_number ||= ""
      self.last_location_search ||= ""
    end

更新

终于找到了一种方法来获取密码_digest ...

这是否提供了有关正在发生的事情的线索?

2.0.0-p481 :016 > Worker.new(password: 'password').encrypted_password
 => ""  # fail
2.0.0-p481 :017 > Worker.new.send(:password_digest, "1234")
 => "$2a$10$mrHUtlUKwKfwIjSJdRjY/.GbLhXKIKOAN9/WVmg3xCXHOJoGJrPMW" 

原始

当我创建工作人员(如用户)时,密码未保存

> Worker.create(email: "test@gmail32.com", password: "1234", password_confirmation: "1234")
=> #<Worker id: 41, email: "test@gmail32.com", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2015-10-01 13:11:27", updated_at: "2015-10-01 13:11:27> 

当我尝试创建一个worker时,会发生这种情况。是什么导致这个?显然,如果你覆盖一个设计方法就会发生 - 这是在这里发生了什么?

这是模型,但我可以注释掉所有内容(“设计”部分除外),但仍然没有保存密码:

require 'resque-scheduler'

class Worker < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :custom_authenticatable, :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  before_save :set_defaults

  def Worker.find_by_credentials(email, password)
    worker = Worker.find_by_email(email.downcase)

    return worker if worker && worker.authenticate(password)

    # worker.errors.add :user_authentication, 'invalid credentials'
    nil
  end

  def authenticate(password)
    live && valid_password?(password)
  end


  def valid_for_custom_authentication?(password)
    live
  end

  private

    def set_defaults
      self.has_accepted_terms = false if has_accepted_terms.nil?
      self.live = false if live.nil?
      self.email.downcase!
    end

end

0 个答案:

没有答案