Rails Tutorial - Errors with User.rb File

后端 未结 1 1179
鱼传尺愫
鱼传尺愫 2021-01-29 04:46

I\'m following the railstutorial.org chapter 7 - I\'m trying to run the app but getting errors with the following code. The error suggests I need another \"end\" at the end of t

1条回答
  •  一生所求
    2021-01-29 05:03

    try this:

    require 'digest'
    
    class User < ActiveRecord::Base  
      attr_accessor :password
        attr_accessible :name, :email, :password, :password_confirmation
    
      email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    
      validates :name,  :presence => true,
                        :length   => { :maximum => 50 }
       validates :email, :presence   => true,
                 :format     => { :with => email_regex },
                 :uniqueness => { :case_sensitive => false }
       validates :password, :presence     => true,
                 :confirmation => true,
                 :length       => { :within => 6..40 }
    
    before_save :encrypt_password
    
    def has_password?(submitted_password) 
      encrypted_password == encrypt(submitted_password)  
    end  
    
    
      private
    
         def encrypt_password
           self.salt = make_salt if new_record?
           self.encrypted_password = encrypt(password)
         end
    
         def encrypt(string)
           secure_hash("#{salt}--#{string}")
         end
    
         def make_salt
           secure_hash("#{Time.now.utc}--#{password}")
         end
    
         def secure_hash(string)
           Digest::SHA2.hexdigest(string)
         end
    
    end
    

    0 讨论(0)
提交回复
热议问题