儘管下面的代碼段起作用,但我已經開始探索一種更好的干預方法。任何人都可以更好地瞭解如何構建它?我感謝您的幫助。幹活動記錄模型驗證
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
麻煩的是,你不重複太多。您可以在AR模型上使用[國際化](http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models)來提取錯誤消息。 'email_address'上需要'allow_blank/nil'嗎? 「存在:真實」還不夠嗎? –
是真的,非常真實。也許我太強調DRY了。我的意思是它仍然可讀。嗯......這可能是矯枉過正。 –
這個問題似乎是脫離主題,因爲它是關於代碼審查 –