【问题标题】:undefined method attr_encrypted in Rails 2.3 appRails 2.3 应用程序中未定义的方法 attr_encrypted
【发布时间】:2015-07-28 01:19:35
【问题描述】:

我正在维护一个 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 中。 所以我在/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


【解决方案1】:

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

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

gem 'attr_encrypted', '1.0.8'

然后看看效果是否更好。 API 可能会因版本而异,因此请务必参考corresponding README file 了解您最终使用的版本。

【讨论】:

  • 这是有道理的——但我已经尝试过 1.0.8、1.0.9、1.1.0——它们都在 Rails 2.3 发布的同时发布——它们都给出了相同的错误,“未定义的方法” attr_encrypted'”。如果我在模型中明确“需要 atrr_encrypted”,则会收到“未初始化的常量 ActiveRecord::VERSION”错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多