kayluhb
kayluhb

Reputation: 658

attr_encrypted with rails

I'm using attr_encrypted in my rails app and it is not working as expected. What am I doing wrong?

My schema looks like this:

ActiveRecord::Schema.define(:version => 20110226214519) do

  create_table "entries", :force => true do |t|
    t.string   "title"
    t.string   "encrypted_username"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

My Model:

class Entry < ActiveRecord::Base

  # Setup accessible (or protected) attributes for your model
  attr_accessible :title, :username
  attr_encrypted :username :key => '&@it)a|S_eouL-hnBq^BJ_!]&A+3pTaw9|N;,kYMD(s.*/UmQD8F|-`HC<#<Qm'

  validates :title, :presence => true
end

then in my console run

e = Entry.new({ :title => 'title' })
e.encrypted_username # returns nil
e.username = "username"
e.encrypted_username # returns nil, but I'm expecting the encrypted value

Then when I run y e, I get

--- !ruby/object:Entry 
attributes: 
  title: title
  encrypted_username: 
  created_at: 
  updated_at: 
attributes_cache: {}

changed_attributes: 
  title: 
destroyed: false
encrypted_username: |
  VHlAnnaz+sPlBXzp95Lvgw==

marked_for_destruction: false
new_record: true
previously_changed: {}

readonly: false
username: username

I see the instance method for the encrypted_username, but it doesn't make it into my db when I save it. Is my issue obvious to anyone out there?

Any insight is greatly appreciated.

Upvotes: 2

Views: 4804

Answers (2)

cgod
cgod

Reputation: 56

I already replied on github, but I'll throw some comments here as well to help anyone else who might run into this issue. In short, there's an issue with the current attr_encrypted gem when running under Rails 3 where it simply never populates the encrypted_ property. It's been fixed in a fork, but there hasn't been any activity on the original project in almost a year.

See https://github.com/shuber/attr_encrypted/issues#issue/2 for more info, and consider giving https://rubygems.org/gems/spectator-attr_encrypted a try.

Upvotes: 4

kayluhb
kayluhb

Reputation: 658

Apparently, this is a known issue with this gem.

See https://github.com/shuber/attr_encrypted/issues#issue/2

Instead, you need to use the forked and updated gem for rails 3 and ruby 1.9.2 here:

https://rubygems.org/gems/spectator-attr_encrypted

Upvotes: 0

Related Questions