2011-01-31 15 views
0

このルビーコードを実行すると、エラーメッセージが表示されます。エラーメッセージは、ファイルの最後に終了タグを探していることを示しています。しかし、私は何の成功もせずに終了タグを追加しようとしました。これはエラーメッセージです:RailsTutorial第7章 - User.rbファイルのエラーメッセージ

# == Schema Information 
# Schema version: <timestamp> 
# 
# Table name: users 
# 
# id   :integer   not null, primary key 
# name  :string(255) 
# email  :string(255) 
# created_at :datetime 
# updated_at :datetime 

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 
+0

[Railsチュートリアルのエラー - User.rbファイルでのエラー](http://stackoverflow.com/questions/4849208/rails-tutorial-errors-with-user-rb-file) –

答えて

2

あなたはあまりにも多くを持っていたendさん。これが動作するかどうかを参照してください:

# == Schema Information 
# Schema version: <timestamp> 
# 
# Table name: users 
# 
# id   :integer   not null, primary key 
# name  :string(255) 
# email  :string(255) 
# created_at :datetime 
# updated_at :datetime 
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 

FYI:それはあなたの書式設定清潔で整然としたを保つために良い方法ですので、読みやすいです。

2
# == Schema Information 
# Schema version: <timestamp> 
# 
# Table name: users 
# 
# id   :integer   not null, primary key 
# name  :string(255) 
# email  :string(255) 
# created_at :datetime 
# updated_at :datetime 

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 

あなたの終わりには、すべてのメソッド宣言の前にいた:

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

これがファイルです。最後に

+0

+1ダン、あなた私にそれを打つ。 –

0

これらは、あなたのuser.rb内部コードのすべての行である場合、あなたは3余分なendを持っていて、不適切にあなたのUserクラスを終えた......

before_saveからコードのブロック定義されたメソッドは、すべてUserクラス内にある必要があります。

関連する問題