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 –