jfrommer jfrommer - 7 months ago 37
Ruby Question

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[user@example.com USER@foo.com A_US-ER@foo.bar.org
first.last@foo.jp alice+bob@baz.cn]
valid_addresses.each do |valid_address|
@user.email = valid_address
assert @user.valid?, "#{valid_address.inspect} should be valid"

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

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:
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

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.