虽然下面的代码段有效,但我已经开始探索更好的方法来干这个了。任何人都更好地了解如何构建这个?我感谢您的帮助。
class User < ActiveRecord::Base
validates :remote_addr, :web_browser, :operating_system, presence: true
validates :age_verification, presence: { message: "You must be at least 18 years old to register for this application" }
validates :email_address,
allow_nil: false,
allow_blank: false,
presence: { message: "A valid email address is required" },
uniqueness: {
case_sensitive: false,
message: "This email address has been previously registered"
},
email_format: {
check_mx: true,
mx_message: "This email address has a bad domain",
message: "This email address must be formatted properly"
}
end
答案 0 :(得分:0)
你可以:
配置/区域设置/ en.yml
en:
errors:
attributes:
age_verification:
blank: 'You must be at least 18 years old to register for this application'
email_address:
blank: 'A valid email address is required'
activerecord:
errors:
messages:
invalid_email_address: 'This email address must be formatted properly'
email_address_not_routable: 'This email address has a bad domain'
activemodel:
errors:
messages:
invalid_email_address: 'This email address must be formatted properly'
email_address_not_routable: 'This email address has a bad domain'
并加入状态验证,age_verification,电子邮件字段和其他
在验证: email_address 中2,确实需要检查 allow_nil 和 allow_blank 以及状态强>?因为验证电子邮件格式与它们重叠,即空白和零不被接受为有效值。对于数据一致性而是使用数据库索引和列约束。
所以我这样做:
validates_presence_of :remote_addr, :web_browser,
:operating_system, :age_verification, :email_address
validates :email_address, uniqueness: { case_sensitive: false },
email_format: { check_mx: true }
将错误消息移至en.yml文件(参见上文),并将create migration添加到表
change_column <tables>, :email, null: false
add_index <tables>, :email, unique: true