我在我的rails应用程序中使用attr_encrypted,它不按预期工作。我究竟做错了什么?attr_encrypted与rails
我的模式是这样的:
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
后来,当我跑你们,我得到
--- !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的实例方法,但是当我保存它时,它并没有将它放到我的db中。我的问题对那里的任何人都很明显吗?
任何洞察力,非常感谢。
不幸的是,我还没有得到旁观者的版本工作。我不断收到attr_encrypted方法的错误。 NoMethodError:未定义的方法'attr_encrypted' – kayluhb 2011-03-08 17:27:41
而且看起来Shuber用attr_encrypted解决了这个问题。所以我只打算使用那个。 – kayluhb 2011-03-08 17:31:55