本文介绍了Rails日期验证与年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的Rails应用程序中,有一个包含DOB字段的注册表单。
用户的年龄不必超过18岁。我如何对此进行验证。
In my rails application there's a signup form which includes a DOB field.
The user's age need not to be more than 18.How can i give validation for this one.
我的dob格式为mm / dd / yy
My dob format is mm/dd/yy
推荐答案
语法非常有趣,您可以执行以下操作:
The syntax for this is pretty fun, you could do something like this:
old_enough = ("10/23/2000".to_date + 18.years) < Date.today
将字符串转换为日期,加上18年,然后看是否在日期之前或
Turn your string into a date, add 18 years, and see if it is before or after today.
如果您想将其放入模型验证器中,此railscast可能会有用:
If you want to put this in a model validator, this railscast could be useful:http://railscasts.com/episodes/211-validations-in-rails-3
这篇关于Rails日期验证与年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!