Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation Railstutorial.org 6.2.4格式验证_Validation_Ruby On Rails 4_Format_Railstutorial.org - Fatal编程技术网

Validation Railstutorial.org 6.2.4格式验证

Validation Railstutorial.org 6.2.4格式验证,validation,ruby-on-rails-4,format,railstutorial.org,Validation,Ruby On Rails 4,Format,Railstutorial.org,在Hartl的Railstutorial.org上工作时,我遇到了一个问题,那就是如何让测试通过电子邮件进行格式验证 My user.rb如下所示: class User < ActiveRecord::Base validates :name, presence: true, length: { maximum:50 } VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z]+\z/i validates :email, presence:

在Hartl的Railstutorial.org上工作时,我遇到了一个问题,那就是如何让测试通过电子邮件进行格式验证

My user.rb如下所示:

class User < ActiveRecord::Base       
  validates :name, presence: true, length: { maximum:50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }
end
class User < ActiveRecord::Base
  validates :name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }
end
我的错误列表如下: 失败:

  1) User
     Failure/Error: it { should be_valid}
       expected #<User id: nil, name: "Example User", email: "user@example.com",
created_at: nil, updated_at: nil> to be valid, but got errors: Email is invalid

     # ./spec/models/user_spec.rb:13:in 'block (2 levels) in <top (required)>'

  2) User when email format is valid should be valid
     Failure/Error: expect(@user).to be_valid
       expected #<User id: nil, name: "Example User", email: "user@foo.COM". cre
ated_at: nil, updated_at: nil> to be valid, but got errors. Email is invalid
     # ./spec/models/user_spec.rb:45:in 'block (4 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:43:in 'each'
     # ./spec/models/user_spec.rb:43:in 'block (3 levels) in >top (required)>'

Finished in 0.03 seconds
8 examples, 2 failures

Failed examples:

rspec ./spec/models/user_spec.rb:13 # User
rspec ./spec/models/user_spec.rb:41 # User when email format is valid should be
valid
1)用户
失败/错误:它{应该是有效的}
应为有效,但出现错误:电子邮件无效
#./spec/models/user_spec.rb:13:in'block(2层)in'
2) 当电子邮件格式有效时,用户应该是有效的
失败/错误:期望(@user).有效
应为有效,但出现错误。电子邮件无效
#./spec/models/user_spec.rb:45:in'block(4层)in'
#./spec/models/user_spec.rb:43:in'each'
#./spec/models/user_spec.rb:43:in'block(3层)in>top(必选)>'
以0.03秒完成
8例,2次失败
失败的示例:
rspec./spec/models/user_spec.rb:13#user
rspec./spec/models/user_spec.rb:41#电子邮件格式有效的用户应
有效的

我确信我遗漏了一些次要的东西(当我很难弄清楚的时候,通常是次要的)。如果能得到任何帮助,我将不胜感激。

本教程中的确切代码如下:

class User < ActiveRecord::Base       
  validates :name, presence: true, length: { maximum:50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }
end
class User < ActiveRecord::Base
  validates :name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }
end
class用户

您的
有效电子邮件\u REGEX=/\A[\w+\-.]+@[A-z]+\z/i
缺少一些字母。

谢谢。我一直在用户规范中寻找答案。非常感谢!