【问题标题】:How to validate birthday using certain conditions. Ruby on rails如何使用特定条件验证生日。轨道上的红宝石
【发布时间】:2017-11-07 10:23:26
【问题描述】:

我目前正在处理我的 ruby​​ on rails 任务,我有这个表格,用户必须在其中输入他们想要加入的部门(初级或高级)和他们的生日。现在,我要做的是根据他们想要加入的部门来验证他们的生日。例如,如果用户选择初级组,他/她必须是 15 到 20 岁,如果是高级,他/她必须是 20 到 25 岁。我尝试了来自互联网的不同语法,但没有任何效果。请帮我。提前致谢。

【问题讨论】:

  • 如果没有您的草稿,我们无法为您提供帮助。
  • 前端还是后端验证?
  • 模型中的那个。
  • 验证 :birthday, :presence =>{:if => {:junior? =>{:message => '你应该在 15 到 20 岁之间。'}}} def midget?() :division == "Junior" end 这就是我现在所拥有的

标签: ruby-on-rails validation


【解决方案1】:

这里有一些逻辑......如果没有某种代码很难看到

您可以通过以下方式计算出他们的年龄:

age = (currentyear - birthday year value)

可能的验证类似于:

if (age > 15 and < 20 and selected=senior) {
 //error you are junior
}
 else if (age > 20 and < 25 and select=junior){
 //error you are senior
}

同样不是一个完整的答案,但看看你是否可以用逻辑和 ruby​​ 语法做一些事情......

【讨论】:

  • validate :validate_birthday private def validate_birthday ifbirthday.present? && Division == "Midget" &&birthday 18.years.ago.to_i errors.add(:birthday, '你应该在 15 到 18 岁才能注册这个部门。' ) 结束
猜你喜欢
  • 1970-01-01
  • 2012-05-30
  • 1970-01-01
  • 2022-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多