2011-01-31 5 views
0

루비 코드를 실행할 때 오류 메시지가 나타납니다. 오류 메시지는 파일 끝에있는 끝 태그를 찾고 있음을 나타냅니다. 그러나, 나는 아무 성공없이 추가적인 끝 꼬리표를 추가하는 시도했다.RailsTutorial 7 장 - User.rb 파일의 오류 메시지

/Users/woshea/rails/sample_app2/app/models/user.rb:57: syntax error, unexpected kEND, expecting $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 } 

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 

를 참고하시기 바랍니다 : 그것은 쉽게 읽을 수 그래서 깨끗하고 질서있는 당신의 서식을 유지하는 좋은 방법입니다.

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 

최종 모든 메소드 선언 전과 이는 오류 메시지입니다. 이 당신의 user.rb 내부 코드의 모든 라인이 경우

+0

+1 젠장, 너 나를 때려. –

0

, 마지막에 before_save에서 ......

코드의 블록을 당신은 3 여분 end이 있고 잘못하여 사용자 클래스를 종료 정의 된 메소드는 모두 User 클래스 내에 있어야합니다.