简体   繁体   English

Michael Hartl教程第6章 - 语法错误

[英]Michael Hartl Tutorial Chapter 6 - Syntax error

I'm working on Michael Hartl's Tutorial. 我正在研究Michael Hartl的教程。 I'm on Chapter 6. I am up to user validation. 我在第6章。我是用户验证。 The code below is what I was told to use, but it doesn't work. 下面的代码是我被告知要使用的,但它不起作用。 I get the error message syntax error, unexpected end-of-input, expecting keyword_end (SyntaxError) . 我收到错误消息syntax error, unexpected end-of-input, expecting keyword_end (SyntaxError) I don't see a syntax error. 我没有看到语法错误。 Any suggestions? 有什么建议么?

require 'spec_helper'

describe User do
  before do
    @user = User.new(name: "Example User", email: "user@example.com")
  end
  subject { @user }
  it { should respond_to(:name) }
  it { should respond_to(:email) }
  it { should respond_to(:password_digest) }
  it { should be_valid }

  describe "when name is not present" do
    before { @user.name = " " }
    it { should_not be_valid }
  end
  describe "when email is not present" do
    before { @user.email = " " }
    it { should_not be_valid }
  end 
  describe "when name is too long" do
    before { @user.name = "a" * 51 }
    it { should_not be_valid }
  end
  describe "when email format is invalid" do
    it "should be invalid" do
      addresses = %w[user@foo,com user_at_foo.org example.user@foo.
                     foo@bar_baz.com foo@bar+baz.com]
      addresses.each do |invalid_address|
        @user.email = invalid_address
        @user.should_not be_valid
      end

  describe "when email format is valid" do
    it "should be valid" do
      addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
      addresses.each do |valid_address|
        @user.email = valid_address
        @user.should be_valid
      end

  describe "when email address is already taken" do
    before do
      user_with_same_email = @user.dup
      user_with_same_email.email = @user.email.upcase
      user_with_same_email.save
    end
    it { should_not be_valid }
  end 
end

I recommend that you indent your code, that way it'll be much more obvious where end keywords should go (and you'll see immediately that you're missing one). 我建议你缩进代码,这样在end关键字应该去的地方会更加明显(你会立刻看到你错过了一个)。 (Hartl does throughout the book.) (哈特尔贯穿整本书。)

For example: 例如:

require 'spec_helper'
describe User do
  before do
    @user = User.new(name: "Example User", email: "user@example.com")
  end
  subject { @user }
  it { should respond_to(:name) }
  it { should respond_to(:email) }
  it { should respond_to(:password_digest) }
  it { should be_valid }
  describe "when name is not present" do
    before { @user.name = " " }
    it { should_not be_valid }
  end
end

If the indentation doesn't match up anywhere, it should jump out at you , then you'll know there's a problem. 如果缩进在任何地方都不匹配, 它应该跳出来 ,然后你会知道有问题。

require 'spec_helper'

describe User do
  before do
    @user = User.new(name: "Example User", email: "user@example.com")
  end
  subject { @user }
  it { should respond_to(:name) }
  it { should respond_to(:email) }
  it { should respond_to(:password_digest) }
  it { should be_valid }

  describe "when name is not present" do
    before { @user.name = " " }
    it { should_not be_valid }
  end
  describe "when email is not present" do
    before { @user.email = " " }
    it { should_not be_valid }
  end 
  describe "when name is too long" do
    before { @user.name = "a" * 51 }
    it { should_not be_valid }
  end
  describe "when email format is invalid" do
    it "should be invalid" do
      addresses = %w[user@foo,com user_at_foo.org example.user@foo.
                     foo@bar_baz.com foo@bar+baz.com]
      addresses.each do |invalid_address|
        @user.email = invalid_address
        @user.should_not be_valid
      end
    end # you've missed it
  end # and this one

  describe "when email format is valid" do
    it "should be valid" do
      addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
      addresses.each do |valid_address|
        @user.email = valid_address
        @user.should be_valid
      end
    end #
  end # and two more    

  describe "when email address is already taken" do
    before do
      user_with_same_email = @user.dup
      user_with_same_email.email = @user.email.upcase
      user_with_same_email.save
    end
    it { should_not be_valid }
  end 
end

In Ruby, do .. end specifies a block. 在Ruby中,do .. end指定一个块。 It goes like: 它像:

... do

.. Some Code Here ...

end

You should indent your code and have better look at the missing end. 您应该缩进代码并更好地查看缺失的结尾。

In general, Its a convention in Ruby to use 2 spaces Or a tab character with length of 2 spaces to indent your code. 一般来说,它在Ruby中使用2个空格或者长度为2个空格的制表符来缩进代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM