2011-02-27 30 views
2

我在我的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中。我的問題對那裏的任何人都很明顯嗎?

任何洞察力,非常感謝。

回答

3

我已經回答GitHub上,但我會在這裏拋出一些評論以幫助任何可能遇到此問題的人。簡而言之,在Rails 3下運行時,當前的attr_encrypted gem存在一個問題,那就是它永遠不會填充encrypted_屬性。它已被固定在一個分支中,但近一年來,原始項目中沒有任何活動。

查看https://github.com/shuber/attr_encrypted/issues#issue/2瞭解更多信息,並考慮給https://rubygems.org/gems/spectator-attr_encrypted一個嘗試。

+3

不幸的是,我還沒有得到旁觀者的版本工作。我不斷收到attr_encrypted方法的錯誤。 NoMethodError:未定義的方法'attr_encrypted' – kayluhb 2011-03-08 17:27:41

+0

而且看起來Shuber用attr_encrypted解決了這個問題。所以我只打算使用那個。 – kayluhb 2011-03-08 17:31:55