jfrommer
jfrommer

Reputation: 21

What am I doing wrong - syntax error unexpected '\n', expecting =>

This is my Ruby code (I am following along in Hartl's Ruby on Rails guide) this is the User_test.rb

test "email validation should accept valid addresses" do
  valid_addresses = %w[[email protected] [email protected] [email protected]
                       [email protected] [email protected]]
  valid_addresses.each do |valid_address|
    @user.email = valid_address
    assert @user.valid?, "#{valid_address.inspect} should be valid"
  end
end

test "email validation should reject invalid addresses" do
  invalid_addresses = %w[user@example,com user_at_foo.org user.name@example.
                         [email protected] foo@bar+baz.com]
  invalid_addresses.each do |invalid_address|
    @user.email = invalid_address
    assert_not @user.valid?, "#{invalid_address.inspect} should be invalid"
  end
end

The error is as follows and I cannot figure out how to fix it - any more experienced people know what I am doing wrong?

 1) Error:
UserTest#test_email_validation_should_reject_invalid_addresses:
SyntaxError: /home/ubuntu/workspace/app/models/user.rb:4: syntax error,       unexpected '\n', expecting =>

Here is the User.rb code

class User < ActiveRecord::Base
  has_many :microposts

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :name, presence: true, length: { maximum: 50},
  validates :email, presence: true, length: { maximum: 255},
  format: { with: VALID_EMAIL_REGEX }, uniqueness: true, case_sensitive: false              
end

Upvotes: 1

Views: 3046

Answers (1)

Eric Duminil
Eric Duminil

Reputation: 54303

validates :name, presence: true, length: { maximum: 50},

You need to remove the ,, Ruby thinks you're continuing the definition of your Hash otherwise.

unexpected '\n', expecting => basically means that Ruby wasn't expecting a new line, but the definition of an Hash with a key followed by a => and a value.

PS: One more reason I don't really like the abbreviated Ruby Hash notation. There's no => to be found in the code, which makes it harder to understand the error message.

Upvotes: 1

Related Questions