我在我的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