2011-01-31 8 views
0

私はrailstutorial.orgの章7に従っています - 私はこのアプリケーションを実行しようとしていますが、次のコードでエラーが発生しています。このエラーは、ファイルの最後に別の "終了"が必要であることを示唆していますが、私はこれを試しましたが、うまくいきませんでした。Railsチュートリアル - User.rbファイルのエラー

エラーは次のとおりです。

/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+\-.][email protected][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 

答えて

2

はこれを試してみてください。

require 'digest' 

class User < ActiveRecord::Base 
    attr_accessor :password 
    attr_accessible :name, :email, :password, :password_confirmation 

    email_regex = /\A[\w+\-.][email protected][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

は、あなたがあなたの答えにしてください何をしたかを説明してもらえますか? –

+3

検証の後でbefore_saveの前に不要な終了を取り除きました。その終わりは基本的にあなたのクラス定義を完成させました。それ以降は、通訳者にとって本当に意味のあるものです。 – brad

+0

それは働いた - ありがとう! –

関連する問題