Advertisement
Guest User

Untitled

a guest
Jul 30th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. !! This is my User.rb model and I randomly I will get the error at the bottom... it will work sometimes and not some others for no apparent reason...
  2.  
  3.  
  4. require 'digest/sha1'
  5. class User < ActiveRecord::Base
  6. # Virtual attribute for the unencrypted password
  7. attr_accessor :password
  8. file_column :image
  9. validates_presence_of :login, :email, :first_name, :last_name
  10. validates_presence_of :password, :if => :password_required?
  11. validates_presence_of :password_confirmation, :if => :password_required?
  12. validates_length_of :password, :within => 5..40, :if => :password_required?
  13. validates_confirmation_of :password, :if => :password_required?
  14. validates_length_of :login, :within => 3..40
  15. validates_length_of :email, :within => 3..100
  16. validates_uniqueness_of :login, :email
  17. validates_format_of :email, :with => %r{^[a-zA-Z0-9@\.]*}i
  18. before_save :encrypt_password
  19. searches_on :first_name, :last_name, :company_name, :email
  20.  
  21. # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
  22. def self.authenticate(login, password)
  23. u = find_by_login(login) # need to get the salt
  24. u && u.authenticated?(password) ? u : nil
  25. end
  26.  
  27. # Encrypts some data with the salt.
  28. def self.encrypt(password, salt)
  29. Digest::SHA1.hexdigest("--#{salt}--#{password}--")
  30. end
  31.  
  32. # Encrypts the password with the user salt
  33. def encrypt(password)
  34. self.class.encrypt(password, salt)
  35. end
  36.  
  37. def authenticated?(password)
  38. crypted_password == encrypt(password)
  39. end
  40.  
  41. protected
  42. # before filter
  43. def encrypt_password
  44. return if password.blank?
  45. self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
  46. self.crypted_password = encrypt(password)
  47. end
  48.  
  49. def password_required?
  50. crypted_password.blank? or not password.blank?
  51. end
  52. end
  53.  
  54. NoMethodError (undefined method `authenticate' for User:Class):
  55. /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1129:in `method_missing'
  56. /app/controllers/account_controller.rb:14:in `login'
  57. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:941:in `send'
  58. /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:941:in `perform_action_without_filters'...
  59. ...
  60. ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement