Rails 使用带有表单和数组的数组验证包含

编程入门 行业动态 更新时间:2024-10-25 18:28:20
本文介绍了Rails 使用带有表单和数组的数组验证包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

尝试就使用数组进行表单选择但使用相同的数组来测试是否包含用于验证的最佳方法获得一些好的建议.

Trying to get some good advice on the best approach to using arrays for form select but using the same array to test inclusion of for the validation.

现在我有了它,但在元素和验证中构建数组,即,

Right now I have it but building the arrays within the elements and validation ie,

# Form <%= f.select(:status, [['Live','live'], ['Paused', 'paused']]) %> # Model validates :status, :inclusion => { :in => %w(live paused) }

我相信会有更好的方法来存储和使用这些数组!

I'm sure there would be a better way to store these arrays and use them !

感谢您提供的任何建议.

Thanks for any advise you can provide.

推荐答案

您可以将这两个常量添加到您的模型中,然后调用验证:

You can add these two constants to your model and then call the validation:

VALID_STATES = ["live", "paused"] SELECT_STATES = VALID_STATES.map { |s| [s.capitalize, s] } validates :status, :inclusion => { :in => Model::VALID_STATES }

更多推荐

Rails 使用带有表单和数组的数组验证包含

本文发布于:2023-10-12 16:35:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485280.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   表单   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!