Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 盐场对所有用户均为零_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 3 盐场对所有用户均为零

Ruby on rails 3 盐场对所有用户均为零,ruby-on-rails-3,Ruby On Rails 3,(ruby 1.9.2,rails 3.1)遵循Michael Hartl的教程。花了一天多的时间试图解决这个问题,重新阅读了这一章,浏览了这么多,到目前为止还没有解决方案。 当我创建用户时,他们都是在DB的盐场中用nil创建的。就像这样: => #<User id: 19, name: "John Doe Fourth", email: "jdoe4@ibm.com", created_at: "2011-12-10 16:36:09", updated_at: "2011- 12

(ruby 1.9.2,rails 3.1)遵循Michael Hartl的教程。花了一天多的时间试图解决这个问题,重新阅读了这一章,浏览了这么多,到目前为止还没有解决方案。 当我创建用户时,他们都是在DB的盐场中用nil创建的。就像这样:

=> #<User id: 19, name: "John Doe Fourth", email: "jdoe4@ibm.com", created_at: "2011-12-10 16:36:09", updated_at: "2011-
12-10 16:36:09", encrypted_password: "5534438b422e928e80479756608b87d33881b5196a28be230c2...", salt: nil>
user.rb

def new
    @user = User.new
    @title = "Sign up"
  end

  def create
    @user = User.new(params[:user])
    if @user.save
            sign_in @user
            flash[:success] = "Welcome to the Blue Bird Microblog!"
            redirect_to @user
      else
      @title = "Sign up"
      render 'new'
    end
  end
require 'digest' class User < ActiveRecord::Base

attr_accessible :name, :email, :password, :password_confirmation
attr_accessor :password, :salt

 before_save :encrypt_password

 def has_password?(submitted_password)
    encrypted_password == encrypt(submitted_password)   
 end

 def self.authenticate(email, submitted_password)
     user = find_by_email(email)
     puts user.inspect
     return nil  if user.nil?
     return user if user.has_password?(submitted_password)   
 end

  def self.authenticate_with_salt(id, cookie_salt)
     user = find_by_id(id)
     (user && user.salt == cookie_salt) ? user : nil   
  end

 private

 def encrypt_password 
       self.salt = make_salt unless has_password?(password)
       self.encrypted_password = encrypt(password) 
     end

  def encrypt(string)
       secure_hash("#{salt}--#{string}")
     end

     def make_salt
       secure_hash("#{Time.now.utc}--#{password}")
     end

     def secure_hash(string)
       Digest::SHA2.hexdigest(string)
     end
require'digest'类用户
您需要删除

attr_accessor :salt
attr_访问器是例如变量,因为salt在数据库中;你需要去掉那条存取线

该死:)我自己想出来了,想和大家分享,但你的答案来了:)我实际上在我的文件和M.Hartl在github上发布的文件之间做了一个长时间的比较。然后我弄明白发生了什么。与你不同,我无法通过查看代码来理解它。我真的很感谢你的回答,非常感谢。