在实施 "has_secure_password" 之后,我在理解我的代码中有什么问题时遇到了一些严重的问题。我的问题是使用“has_secure_password”并创建新用户。
我正在使用导轨 4
宝石文件
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.5'
gem 'mysql2', '>= 0.3.13', '< 0.5'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'bcrypt', '~> 3.1.7'
2.2.3 :034 > user=User.new(name: "user2", email: "user2@mail.com", password: "1qaz!QAZ", password_confirmation: "1qaz!QAZ") => # 2.2.3 :035 > 用户保存!(0.4ms) BEGIN User Exists (0.3ms) SELECT 1 AS one FROM users
WHERE users
。email
= 'user2@mail.com' LIMIT 1 (0.1ms) ROLLBACK ActiveRecord::RecordInvalid:验证失败:密码不能为空
我的用户模型
class User < ActiveRecord::Base
attr_accessor :password, :password_confirmation, :remember_token
has_secure_password
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
before_save { email.downcase! }
validates :password, length: { minimum: 6 }
end
请帮助我理解我的代码有什么问题。