我正在维护一个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,并将其添加到config.load_paths
中的environment.rb
行。
但我仍然得到'未定义的方法attr_encrypted'。
我已经没事了。有什么想法吗?
答案 0 :(得分:0)
I believe your issue is that the attr_encrypted
gem is (at its current version) not compatible with Rails 2.3. Per your comment, Bundler is skipping loading the gem because it raises the error NameError: uninitialized constant ActiveRecord::VERSION
when it does so, which is why none of the gem's functionality is available in your app.
However, the gem was originally created back in 2009, so likely an older version of it will be compatible with your Rails. Try updating your Gemfile to use one of the older versions, e.g.:
gem 'attr_encrypted', '1.0.8'
And see if that works any better. The API might change between the versions, so make sure to refer to the corresponding README file for the version you end up using.