0
我想在第6章Michael Hartl的rails教程中做正则表达式电子邮件验证示例,但由于某些原因测试“电子邮件验证应接受有效地址”失败有效的电子邮件地址。按照建议,我尝试使用Rubular的正则表达式,并通过那里。如果有人知道它为什么在我的rails代码中失败了,我会很感激帮助解决它。谢谢。Ruby on Rails - 正则表达式电子邮件验证不起作用
测试失败
test "email validation should accept valid addresses" do
valid_addresses = %w[[email protected] [email protected] [email protected]]
valid_addresses.each do |valid_address|
@user.email = valid_addresses
assert @user.valid?, "#{valid_address.inspect} should be valid"
end
end
用户类正则表达式和验证定义
class User < ActiveRecord::Base
has_many :posts, dependent: :destroy
has_many :text_posts, dependent: :destroy
has_many :image_posts, dependent: :destroy
validates :name, presence: true, length: {maximum: 50}
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: {maximum: 255}, format: {with: VALID_EMAIL_REGEX}
end
少自己,但更多的麻烦免费https://github.com/balexand/email_validator –