0

我正在维护一个 Rails 2.3 应用程序(迁移到 Rails 4 破坏了太多东西),并尝试加密/解密密码而不是将其以纯文本形式保存在数据库中。

按照 attr_encrypted gem 的说明,我添加了

gem "attr_encrypted"

到我的 Gemfile,运行 bundle install - 一切都很开心。

然后,按照说明,我将新字段 encrypted_pa​​ssword 迁移到表中,并在 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 中。所以我在 中找到了 attr_encrypted gem /var/lib/gems/1.8/gems/,并将其添加config.load_pathsenvironment.rb.

但我仍然得到“未定义的方法 attr_encrypted”。

我已经没有东西可以尝试了。有任何想法吗?

4

1 回答 1

0

我相信您的问题是attr_encryptedgem(在其当前版本)与 Rails 2.3 不兼容。根据您的评论,Bundler 正在跳过加载 gem,因为这样做会引发错误NameError: uninitialized constant ActiveRecord::VERSION,这就是为什么在您的应用程序中没有 gem 的功能可用的原因。

但是,gem 最初是在 2009 年创建的,因此它的旧版本很可能您的 Rails 兼容。尝试更新您的 Gemfile 以使用旧版本之一,例如:

gem 'attr_encrypted', '1.0.8'

看看是否效果更好。API 可能会在版本之间发生变化,因此请务必参考您最终使用的版本的相应 README 文件。

于 2015-07-28T16:05:14.047 回答