RailsTutorial第7章 - User.rb文件的错误消息

时间:2011-01-31 08:35:25

标签: ruby-on-rails

当我运行这段ruby代码时,我收到一条错误消息。错误消息表明它正在文件末尾查找结束标记。但是,我尝试添加其他结束标记但没有成功。这是错误消息:

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

3 个答案:

答案 0 :(得分: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+\-.]+@[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

您的结束是在所有方法声明之前。

答案 1 :(得分: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+\-.]+@[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 :(得分:0)

如果这些是你的user.rb中的所有代码行,那么你有3个额外的end并且你不正确地结束了你的用户类......

before_save到最后定义的方法的代码块都应该在User类中。