ruby-on-rails – 在Ruby类中干掉条件验证

ruby-on-rails – 在Ruby类中干掉条件验证,第1张

概述如何清理我的验证码?我有一个讨论模型,它有一个类别和状态字段.状态值取决于类别值.例如,类别==’问题’的讨论只能在状态[:问题]中具有状态. STATUSES = { question: %w[answered], suggestion: %w[pending planned started completed declined], problem: %w[ 如何清理我的验证码?我有一个讨论模型,它有一个类别和状态字段.状态值取决于类别值.例如,类别==’问题’的讨论只能在状态[:问题]中具有状态.

STATUSES   = {    question:   %w[answered],suggestion: %w[pending planned started completed declined],problem:    %w[started solved]  }  valIDates :status,allow_blank: true,inclusion: { in: STATUSES[:question] },if: lambda { self.category == 'question' }  valIDates :status,inclusion: { in: STATUSES[:suggestion] },if: lambda { self.category == 'suggestion' }  valIDates :status,inclusion: { in: STATUSES[:problem] },if: lambda { self.category == 'problem' }

我正在使用Rails 3.

解决方法 :include:in接受lambda本身:

valIDates :status,inclusion: { in: lambda { |o| STATUSES[o.category.to_sym] } }

文档:http://apidock.com/rails/ActiveModel/Validations/HelperMethods/validates_inclusion_of

总结

以上是内存溢出为你收集整理的ruby-on-rails – 在Ruby类中干掉条件验证全部内容,希望文章能够帮你解决ruby-on-rails – 在Ruby类中干掉条件验证所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1280964.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存