Ruby on rails RoR:attr#u加密分配不';你没写吗?
我正在使用RoR 3.0.5中的Ruby on rails RoR:attr#u加密分配不';你没写吗?,ruby-on-rails,ruby-on-rails-3,activerecord,encryption,Ruby On Rails,Ruby On Rails 3,Activerecord,Encryption,我正在使用RoR 3.0.5中的attr_encrypted(v1.2.0)加密我不希望在数据库中以纯文本形式显示的凭据。当我更新加密字段时,它似乎没有保存到数据库中 我的模型基本上是: class Service << ActiveRecord::Base attr_encrypted :credentials_aux, :key => KEY, :attribute => 'encrypted_credentials', :encode => true,
attr_encrypted
(v1.2.0)加密我不希望在数据库中以纯文本形式显示的凭据。当我更新加密字段时,它似乎没有保存到数据库中
我的模型基本上是:
class Service << ActiveRecord::Base
attr_encrypted :credentials_aux, :key => KEY, :attribute => 'encrypted_credentials', :encode => true, :marshal => true
def credentials
credentials_aux
end
def credentials=(c)
h = {}.update(c) # coerce HashWithIndifferentAccess to a vanilla hash
credentials_aux = h
end
...
end
class服务密钥,:attribute=>encrypted\u credentials',:encode=>true,:marshal=>true
def凭证
全权证书
结束
def凭证=(c)
h={}.update(c)#强制hashWithDisference访问普通散列
凭证_aux=h
结束
...
结束
(请注意,'credentials='方法的存在只是为了将Rails生成的HashWithInferenceTaccess强制转换为普通哈希。它还为我提供了插入调试打印输出以验证数据的位置。)
但当我尝试通过控制台更新凭据时,不需要:
>> s = Service.find(19)
=> #<Service id: 19, encrypted_credentials: "10VfHU7IkdrFb4Q6Hj18YtY81rbRp3sIuoVUl8CHNj88cq1XFo2...",>
>> s.credentials
=> {"user_id"=>"fred.flintstone", "password"=>"supersecret"}
>> s.credentials = {"user_id" => "barney.rubble", "password" => "notsosecret"}
=> {"user_id" => "barney.rubble", "password" => "notsosecret"}
>> s.credentials
=> {"user_id"=>"fred.flintstone", "password"=>"supersecret"}
>s=Service.find(19)
=> #
>>美国证书
=>{“用户id”=>“fred.flintstone”,“密码”=>“超级机密”}
>>s.credentials={“user_id”=>“barney.browse”,“password”=>“notsosecret”}
=>{“用户id”=>“barney.browse”,“密码”=>“notsosecret”}
>>美国证书
=>{“用户id”=>“fred.flintstone”,“密码”=>“超级机密”}
为什么没有将
s.credentials
更新为新值?我相信您只是在“credentials=”方法中设置了一个名为“credentials\u aux”的局部变量,请尝试显式使用“self”
实际上,我是该项目的维护者,因此,如果上述修复不起作用,我将为此开出一张新的罚单。您没有比这更好的服务了。我像冠军一样工作-非常感谢您的及时回复(以及令人敬畏的宝石)@布雷特:说真的!顺便问一下,我将hashwithinterference转换为常规hash的方法是否合理?或者有更简单的方法吗?@fearless\u傻子,你可以试试用
def credentials=(c)
h = {}.update(c) # coerce HashWithIndifferentAccess to a vanilla hash
self.credentials_aux = h
end