gpt4 book ai didi

ruby-on-rails - rails : Validates_format_of for float not working

转载 作者:行者123 更新时间:2023-12-04 16:48:43 30 4
gpt4 key购买 nike

我是 Ruby on Rails 的新手。
我试图验证属性之一的格式以仅输入浮点数。

validates :price, :format => { :with => /^[0-9]{1,5}((\.[0-9]{1,5})?)$/, :message => "should be float" }

但是当我在价格中只输入字符时,它会接受它并显示 0.0 的价格值。
任何人都可以告诉,这有什么问题或为什么会发生这种情况?

最佳答案

这是我的解决方案
validates :price,presence:true, numericality: {only_float: true}
当您填写例如 7 时,它会自动将值传输到 7.0

关于ruby-on-rails - rails : Validates_format_of for float not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8137571/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com