比如像这样:user => addition_info: {qq: 33333, email: [email protected] } 我想验证 Email 是否合法,该如何做?
要用校验应该平铺在 User Model 或者抽取成 AdditionInfo Model。
user.rb
class User validate :check_addition_email_info private def check_addition_email_info if not /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i.match(email) errors.add(:addition_info, "email invalid!") end end end
未经验证 :)
#2 楼 @ruby_sky 我来检测一下吧。