http://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html
使用 has_secure_password
的时候,应该要输入 password_confirmation
才可以 创建新用户成功的,为什么我只用了一个user.password = "bar"
,然后就生成新用户成功了呢?
.1.2 :001 > user = User.create(username: "foo")
(0.1ms) begin transaction
(0.1ms) rollback transaction
=> #<User id: nil, username: "foo", created_at: nil, updated_at: nil, password_digest: nil>
2.1.2 :002 > user.password = "bar"
=> "bar"
2.1.2 :003 > user.save
(0.3ms) begin transaction
Binary data inserted for `string` type on column `password_digest`
SQL (0.5ms) INSERT INTO "users" ("created_at", "password_digest", "updated_at", "username") VALUES (?, ?, ?, ?) [["created_at", "2014-06-23 13:42:10.466978"], ["password_digest", "$2a$10$cecQPWxE3oyi78fU9FxFN.SQ1buu/jXY6gHF3tPwybRMQfNJg5tS6"], ["updated_at", "2014-06-23 13:42:10.466978"], ["username", "foo"]]
(2.1ms) commit transaction
=> true
Gemfile
1 source 'https://rubygems.org'
2
3 gem 'rails', '4.1.1'
4 gem 'sass-rails', '~> 4.0.0'
5 gem 'uglifier', '>= 1.3.0'
6 gem 'coffee-rails', '~> 4.0.0'
7 gem 'bootstrap-sass', '~> 2.3.2.0'
8 gem 'bcrypt'
9
10
11
12 gem 'jquery-rails'
13 gem 'turbolinks'
14 gem 'jbuilder', '~> 1.2'
15
16 group :doc do
17 gem 'sdoc', require: false
18 end
19
20 group :development do
21 gem 'quiet_assets'
22 gem 'pry'
23 gem 'sqlite3'
24 #gem 'mysql2'
25 end
26
27 group :production do
28 gem 'pg'
29 gem 'rails_12factor'
30 end
Model
user.rb
1 class User < ActiveRecord::Base
2 has_many :posts
3 has_many :comments
4
5 has_secure_password
6 end