attr_encrypted with rails

时间:2011-02-27 00:09:11

标签: ruby-on-rails ruby-on-rails-3 hash encryption

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

然后,当我跑步时,我得到了

--- !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的实例方法,但是当我保存它时它没有进入我的数据库。我的问题对任何人都很明显吗?

非常感谢任何见解。

2 个答案:

答案 0 :(得分:4)

我已经回复了github,但我会在这里发表一些评论,以帮助其他可能遇到此问题的人。简而言之,当在Rails 3下运行时,当前的attr_encrypted gem存在问题,它根本不会填充encrypted_属性。它已在一个分支中修复,但近一年内原始项目没有任何活动。

有关详细信息,请参阅https://github.com/shuber/attr_encrypted/issues#issue/2,并考虑尝试https://rubygems.org/gems/spectator-attr_encrypted

答案 1 :(得分:0)

显然,这是这个宝石的一个已知问题。

请参阅https://github.com/shuber/attr_encrypted/issues#issue/2

相反,你需要在rails 3和ruby 1.9.2中使用forked和updated gem:

https://rubygems.org/gems/spectator-attr_encrypted

相关问题