【发布时间】:2011-07-05 01:46:52
【问题描述】:
我在我的 rails 应用程序中使用 attr_encrypted,但它没有按预期工作。我做错了什么?
我的架构如下所示:
ActiveRecord::Schema.define(:version => 20110226214519) do
create_table "entries", :force => true do |t|
t.string "title"
t.string "encrypted_username"
t.datetime "created_at"
t.datetime "updated_at"
end
end
我的模特:
class Entry < ActiveRecord::Base
# Setup accessible (or protected) attributes for your model
attr_accessible :title, :username
attr_encrypted :username :key => '&@it)a|S_eouL-hnBq^BJ_!]&A+3pTaw9|N;,kYMD(s.*/UmQD8F|-`HC<#<Qm'
validates :title, :presence => true
end
然后在我的控制台中运行
e = Entry.new({ :title => 'title' })
e.encrypted_username # returns nil
e.username = "username"
e.encrypted_username # returns nil, but I'm expecting the encrypted value
然后当我运行 y e 时,我得到了
--- !ruby/object:Entry
attributes:
title: title
encrypted_username:
created_at:
updated_at:
attributes_cache: {}
changed_attributes:
title:
destroyed: false
encrypted_username: |
VHlAnnaz+sPlBXzp95Lvgw==
marked_for_destruction: false
new_record: true
previously_changed: {}
readonly: false
username: username
我看到了 encrypted_username 的实例方法,但是当我保存它时它没有进入我的数据库。我的问题对任何人来说都很明显吗?
非常感谢任何见解。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 hash encryption