Rails 教程 - User.rb 文件错误 [英] Rails Tutorial - Errors with User.rb File

查看:29
本文介绍了Rails 教程 - User.rb 文件错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 railstutorial.org 第 7 章 - 我正在尝试运行该应用程序,但出现以下代码错误.该错误表明我需要在文件末尾添加另一个结尾"——但我已经尝试过了,但没有奏效.

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 the file -- but I've tried this and it hasn't worked.

错误是:

/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

推荐答案

试试这个:

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

这篇关于Rails 教程 - User.rb 文件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆