【问题标题】:attr_encrypted with railsattr_encrypted 与 rails
【发布时间】:2011-07-05 01:46:52
【问题描述】:

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

然后当我运行 y e 时,我得到了

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

非常感谢任何见解。

【问题讨论】:

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


    【解决方案1】:

    我已经在 github 上回复了,但是我也会在这里扔一些 cmets 来帮助其他可能遇到这个问题的人。简而言之,当前 attr_encrypted gem 在 Rails 3 下运行时存在一个问题,它根本不会填充 encrypted_ 属性。它已在 fork 中修复,但在近一年的时间里,原始项目没有任何活动。

    请参阅https://github.com/shuber/attr_encrypted/issues#issue/2 了解更多信息,并考虑试试https://rubygems.org/gems/spectator-attr_encrypted

    【讨论】:

    • 不幸的是,我也无法让旁观者的版本工作。我不断收到 attr_encrypted 方法的错误。 NoMethodError:未定义的方法`attr_encrypted'
    • 看起来 Shuber 用 attr_encrypted 解决了这个问题。所以我只使用那个。
    【解决方案2】:

    显然,这是此 gem 的一个已知问题。

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

    相反,您需要在此处为​​ rails 3 和 ruby​​ 1.9.2 使用分叉和更新的 gem:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      相关资源
      最近更新 更多