Ruby on rails Rails教程-User.rb文件错误

Ruby on rails Rails教程-User.rb文件错误,ruby-on-rails,Ruby On Rails,我遵循railstutorial.org第7章-我试图运行应用程序,但以下代码出现错误。这个错误表明我需要在文件的末尾添加另一个“end”——但我已经尝试过了,但没有成功 错误是: /Users/woshea/rails/sample_app2/app/models/user.rb:58: syntax error, unexpected kEND, expecting $end 代码如下: require 'digest' class User < ActiveRecord::Bas

我遵循railstutorial.org第7章-我试图运行应用程序,但以下代码出现错误。这个错误表明我需要在文件的末尾添加另一个“end”——但我已经尝试过了,但没有成功

错误是:

/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
需要“摘要”
类用户true,
:length=>{:max=>50}
验证:email,:presence=>true,
:format=>{:with=>email_regex},
:唯一性=>{:区分大小写=>false}
验证:password,:presence=>true,
:confirmation=>true,
:length=>{:within=>6..40}
结束
保存前:加密密码
def有密码?(已提交密码)
加密密码==加密(已提交密码)
结束
私有的
def加密密码
self.salt=如果有新记录,则制作盐?
self.encrypted_password=加密(密码)
结束
def加密(字符串)
安全散列(“#{salt}--#{string}”)
结束
def制盐
安全散列(“#{Time.now.utc}--#{password}”)
结束
def secure_散列(字符串)
摘要::SHA2.hexdigest(字符串)
结束
结束
结束
结束
试试这个:

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
需要“摘要”
类用户true,
:length=>{:max=>50}
验证:email,:presence=>true,
:format=>{:with=>email_regex},
:唯一性=>{:区分大小写=>false}
验证:password,:presence=>true,
:confirmation=>true,
:length=>{:within=>6..40}
保存前:加密密码
def有密码?(已提交密码)
加密密码==加密(已提交密码)
结束
私有的
def加密密码
self.salt=如果有新记录,则制作盐?
self.encrypted_password=加密(密码)
结束
def加密(字符串)
安全散列(“#{salt}--#{string}”)
结束
def制盐
安全散列(“#{Time.now.utc}--#{password}”)
结束
def secure_散列(字符串)
摘要::SHA2.hexdigest(字符串)
结束
结束

你能解释一下你在回答中做了什么吗?在验证之后和保存之前,他去掉了不必要的结尾。这一结束基本上完成了类定义,因此对于解释器来说,在这之后没有什么是真正有意义的。