2011-02-27 53 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