这是源代码:
def sign_in(user)
remember_token = User.new_remember_token
#等效于:cookies[:remember_token] = { value: remember_token,expires: 20.years.from_now.utc }
cookies.permanent[:remember_token]=remember_token
remember_token2 =User.encrypt(remember_token)
puts ‘save remember_token1----------------->>:',remember_token2
user.update_attribute(:remember_token , remember_token2 );
self.current_user=user
end
def current_user
remember_token = User.encrypt(cookies[:remember_token])
puts 'find remember_token3----------------->>:',remember_token
puts 'if it find',User.find_by(remember_token: remember_token)
@current_user||= User.find_by(remember_token: remember_token)
end
def User.new_remember_token
SecureRandom.urlsafe_base64
end
def User.encrypt(token)
Digest::SHA1.hexdigest(token.to_s)
end
然后运行调用 sign_in 函数,传入一个 user:出现的结果:
save remember_token1----------------->>:
aaf9c6a67eafc3f77e9e5f73bcd65f6d8f2d3948
(0.1ms) begin transaction
Binary data inserted for string
type on column remember_token
SQL (0.5ms) UPDATE "users" SET "remember_token" = ?, "updated_at" = ? WHERE "users"."id" = 1 [["remember_token", "ac125e21202b352dbc32d654fa71794fb0b1df01"], ["updated_at", Sat, 09 Nov 2013 01:59:54 UTC +00:00]]
ps:为嘛 update 的时候,remember_token 它就变了呢,泪奔啊!!!!!
(151.8ms) commit transaction
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "1"]]
Rendered users/show.html.erb within layouts/application (0.3ms)
find remember_token3----------------->>:
aaf9c6a67eafc3f77e9e5f73bcd65f6d8f2d3948
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'aaf9c6a67eafc3f77e9e5f73bcd65f6d8f2d3948' LIMIT 1
if it find
注意最后一行是空的,说明没找到,google 了一下,说是编码问题,反正没解决