william tell
william tell

Reputation: 4502

RailsTutorial Chapter 7 - Error Message for User.rb File

I'm getting an error message when I run this piece of ruby code. The error message suggests it is looking for an end tag at the end of the file. However, I have tried adding additional end tags without any success. This is the error message:

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

This is the file:

# == 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

Upvotes: 0

Views: 553

Answers (3)

Mike Bethany
Mike Bethany

Reputation:

You had way too many end's. See if this works:

# == 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

FYI: It's good practice to keep your formatting clean and orderly so it's easier to read.

Upvotes: 2

PeterWong
PeterWong

Reputation: 16011

If these are all the lines of codes inside your user.rb, then you have 3 extra end and you ended your User class improperly......

The block of codes from before_save to the last method defined should all be inside the User class.

Upvotes: 0

Gazler
Gazler

Reputation: 84180

# == 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

Your end was before all the method declarations.

Upvotes: 2

Related Questions