0
votes

I'm maintaining a Rails 2.3 app (moving to Rails 4 broke too many things), and trying to encrypt/decrypt a password instead of saving it plain-text in the database.

Following the instructions for the attr_encrypted gem, I added

gem "attr_encrypted"

to my Gemfile, ran bundle install - all happy.

Then, per instructions, I migrated in a new field, encrypted_password, to the table, and put one line in app/models/serverobj.rb:

attr_encrypted :password, :key => 'foo'

But when I browse there, I get a stack trace like this:

=> 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, line 17 is:

attr_encrypted :password, :key => 'foo'

environment.rb, line 70 is:

Rails::Initializer.run do |config|
  config.load_paths += %W( #{RAILS_ROOT}/vendor/gems/ #{RAILS_ROOT}/app/exceptions/ )

Aha! I said, the attr_encrypted gem must not be in the load_paths. So I found the attr_encrypted gem in /var/lib/gems/1.8/gems/, and added that to the config.load_paths line in environment.rb.

But I still get 'Undefined method attr_encrypted'.

I've run out of things to try. Any ideas?

1
If you run your rails console, does class Foo; attr_encrypted :bar; end raise the same error? Does require 'attr_encrypted' work? Does the first snippet work after doing so?Robert Nubel
Thanks - "script/console" immediately says 'undefined method attr_encrypted'. And yes I get the same error when I enter your Class Foo... lines. And if I 'require 'attr_encrypted', I get "NameError: uninitialized constant ActiveRecord::VERSION", and a stack trace. Does this point you anywhere useful?Lindsay Morris

1 Answers

0
votes

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.