当用户登录的时候会 我需要生成一个随机密钥存到数据库和 cookie 里
cookies.permanent[:remember_token] = SecureRandom.urlsafe_base64
user.update_attributes(remember_token: cookies.permanent[:remember_token])
这是一开始的代码 会出现一个undefined method '[]' for nil:NilClass
错误 找了半天也没看到问题于是修改了一下代码
user.update_attributes(remember_token: SecureRandom.urlsafe_base64)
cookies.permanent[:remember_token] = user.remember_token
就没有问题了
但是两段代码从语义上来说没有区别 不懂怎么会提示 [] 为方法的错误...
然后把cookies.permanent[:remember_token]
编程"#{cookies.permanent[:remember_token]}"
还是提示同样的错误
cookies.inspect:
#<ActionDispatch::Cookies::CookieJar:0x007fb7a421a8c8 @secret="180c55c6cb9f2840156bdbf337b97ec1d3d2f8bb56dd3ca144a2f1630fb932d907bf930ddf677c296b32f613cc121f0e4efeaaaef154d760aa1312bd77509f0b", @set_cookies={}, @delete_cookies={}, @host="0.0.0.0", @secure=false, @closed=false, @cookies={"_baiwale_session"=>"BAh7B0kiD3Nlc3Npb25faWQGOgZFVEkiJTNhMjRmMzNhODZlNWI5MTBkNTk3NWEyZDhlZDg3ODcxBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMU9wTmZNUUlRbnVodkc0dGl0aDU3dkd5QUc5RHQwSDdjZXpHZTBIL1dNSlE9BjsARg==--c3e3059d6a5a0a51e0dd8bbd67e887cf7b6acd8b", "remember_token"=>"GQEip8f8XlryHzNu0T986Q"}, @signed=#<ActionDispatch::Cookies::SignedCookieJar:0x007fb7a423baf0 @parent_jar=#<ActionDispatch::Cookies::CookieJar:0x007fb7a421a8c8 ...>, @verifier=#<ActiveSupport::MessageVerifier:0x007fb7a423be10 @secret="180c55c6cb9f2840156bdbf337b97ec1d3d2f8bb56dd3ca144a2f1630fb932d907bf930ddf677c296b32f613cc121f0e4efeaaaef154d760aa1312bd77509f0b", @digest="SHA1", @serializer=Marshal>>>
------------------------------
cookies.permanent.inspect:
#<ActionDispatch::Cookies::PermanentCookieJar:0x007fb7a61b26b8 @parent_jar=#<ActionDispatch::Cookies::CookieJar:0x007fb7a421a8c8 @secret="180c55c6cb9f2840156bdbf337b97ec1d3d2f8bb56dd3ca144a2f1630fb932d907bf930ddf677c296b32f613cc121f0e4efeaaaef154d760aa1312bd77509f0b", @set_cookies={}, @delete_cookies={}, @host="0.0.0.0", @secure=false, @closed=false, @cookies={"_baiwale_session"=>"BAh7B0kiD3Nlc3Npb25faWQGOgZFVEkiJTNhMjRmMzNhODZlNWI5MTBkNTk3NWEyZDhlZDg3ODcxBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMU9wTmZNUUlRbnVodkc0dGl0aDU3dkd5QUc5RHQwSDdjZXpHZTBIL1dNSlE9BjsARg==--c3e3059d6a5a0a51e0dd8bbd67e887cf7b6acd8b", "remember_token"=>"GQEip8f8XlryHzNu0T986Q"}, @signed=#<ActionDispatch::Cookies::SignedCookieJar:0x007fb7a423baf0 @parent_jar=#<ActionDispatch::Cookies::CookieJar:0x007fb7a421a8c8 ...>, @verifier=#<ActiveSupport::MessageVerifier:0x007fb7a423be10 @secret="180c55c6cb9f2840156bdbf337b97ec1d3d2f8bb56dd3ca144a2f1630fb932d907bf930ddf677c296b32f613cc121f0e4efeaaaef154d760aa1312bd77509f0b", @digest="SHA1", @serializer=Marshal>>, @permanent=#<ActionDispatch::Cookies::PermanentCookieJar:0x007fb7a61b26b8 ...>>, @secret="180c55c6cb9f2840156bdbf337b97ec1d3d2f8bb56dd3ca144a2f1630fb932d907bf930ddf677c296b32f613cc121f0e4efeaaaef154d760aa1312bd77509f0b">
#14 楼 @2forVendetta 嗯 cookies.permanent
方法创建一个 PermanentCookieJar 对象,虽然后者继承自 CookieJar,继承了[]
方法,但是[]
里引用的是@cookies
,这个对象在 PermanentCookieJar 没有被初始化过,所以出现了 nil 错误。如果希望修复的话,其实只要在 PermanentCookieJar 实现一个[]
方法,里面引用@parent_jar
就可以了
#14 楼 @2forVendetta 逻辑上 cookies.permanent 也只能实现 setter,不太好实现 getter 吧…… 因为浏览器端本身没有真正永久的 cookies,cookies.permanent 应该只是将过期时间设置为当前时间加上一个比较大的值,反过来要 get 的时候就没法判断 permanent 什么的,到头来还是直接按 cookies[:key] 来读取,于是也没实现的必要
#21 楼 @2forVendetta #22 楼 @iBachue 文档里也有写 cookies.permanent # Sets a "permanent" cookie (which expires in 20 years from now).
没有 [] 只有 []= 确实是挺坑