Rails教程 - User.rb文件的错误

时间:2011-01-31 09:20:51

标签: ruby-on-rails

我正在关注railstutorial.org第7章 - 我正在尝试运行应用程序但是使用以下代码获取错误。错误表明我需要在文件末尾另一个“结束” - 但我已经尝试过这个并且它没有用。

错误是:

/Users/woshea/rails/sample_app2/app/models/user.rb:58: syntax error, unexpected kEND, expecting $end

代码在这里:

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 }

end

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
end
end

1 个答案:

答案 0 :(得分:2)

试试这个:

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