【发布时间】:2015-07-28 01:19:35
【问题描述】:
我正在维护一个 Rails 2.3 应用程序(迁移到 Rails 4 破坏了太多东西),并尝试加密/解密密码而不是将其以纯文本形式保存在数据库中。
按照 attr_encrypted gem 的说明,我添加了
gem "attr_encrypted"
在我的 Gemfile 中,运行 bundle install - 一切顺利。
然后,按照说明,我将新字段 encrypted_password 迁移到表中,并在 app/models/serverobj.rb 中放入一行:
attr_encrypted :password, :key => 'foo'
但是当我在那里浏览时,我会得到这样的堆栈跟踪:
=> Booting WEBrick...
/home/art/vendor/rails/activerecord/lib/active_record/base.rb:1833:in `method_missing_without_paginate': undefined method `attr_encrypted' for #<Class:0x7f009f372200> (NoMethodError)
from /home/art/vendor/plugins/will_paginate/lib/will_paginate/finder.rb:164:in `method_missing'
from /home/art/app/models/serverobj.rb:17
...
from /home/art/config/environment.rb:70
serverobj.rb,第 17 行是:
attr_encrypted :password, :key => 'foo'
environment.rb,第 70 行是:
Rails::Initializer.run do |config|
config.load_paths += %W( #{RAILS_ROOT}/vendor/gems/ #{RAILS_ROOT}/app/exceptions/ )
啊哈!我说过,attr_encrypted gem 一定不能在 load_paths 中。
所以我在/var/lib/gems/1.8/gems/ 中找到了attr_encrypted gem,并将其添加到environment.rb 中的config.load_paths 行。
但我仍然得到“未定义的方法 attr_encrypted”。
我已经没有东西可以尝试了。有什么想法吗?
【问题讨论】:
-
如果你运行你的 rails 控制台,
class Foo; attr_encrypted :bar; end会引发同样的错误吗?require 'attr_encrypted'有效吗?这样做后第一个sn-p是否有效? -
谢谢 - “脚本/控制台”立即说“未定义的方法 attr_encrypted”。是的,当我输入您的 Class Foo... 行时,我得到了同样的错误。如果我'需要'attr_encrypted',我会得到“NameError: uninitialized constant ActiveRecord::VERSION”和一个堆栈跟踪。这点对你有用吗?
标签: ruby-on-rails attr-encrypted