gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:54:34 24 4
gpt4 key购买 nike

我正在编写 Michael Hartl 的教程。我在第 6 章。我要进行用户验证。下面的代码是我被告知要使用的代码,但它不起作用。我收到错误消息 syntax error, unexpected end-of-input, expecting keyword_end (SyntaxError)。我没有看到语法错误。有什么建议吗?

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

最佳答案

我建议您缩进您的代码,这样它会更加明显 end 关键字应该放在哪里(您会立即发现您遗漏了一个)。 (Hartl 在整本书中都这样做。)

例如:

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

如果缩进在任何地方都不匹配,它应该跳到你面前,然后你就会知道有问题。

关于ruby-on-rails - Michael Hartl 教程第 6 章 - 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16723170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com