Rails range_field 从 0 到 -100 [英] Rails range_field from 0 to -100

查看:66
本文介绍了Rails range_field 从 0 到 -100的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个范围字段,当向右滑动时,需要使输出更负"达到 -100.

I have a range field that, when slid right, needs to make the output "more negative" up to -100.

这是我当前的代码:

<%= f.range_field :Question1, :in => 0..-100, :step => '.1' %>

显然,使用 :in => -100..0 工作正常,但是当我将其切换到 0..-100 时,向右滑动会上升到 100,这显然是错误的.我需要用户能够向右滑动以获得更多的负数.有谁知道如何做到这一点?

Obviously, using :in => -100..0 works fine, but when I switch it to 0..-100, sliding to the right goes up to 100, which is clearly wrong. I need the user to be able to slide right in order to get a more negative number. Does anyone know how to make this happen?

如果这是不可能的,有没有办法设置 :in => 0..100 然后在将其放入数据库之前将数字设为负数?

If this is not possible, is there a way to set up :in => 0..100 but then make the number negative before it's put into the database?

推荐答案

我相信您可以使用 before_save 过滤器在保存之前否定该值,或者使用 before_validation在它验证之前否定它,然后你可以验证你得到的数字是你想要的.

I believe you could use a before_save filter to negate the value before saving it, or a before_validation to negate it before it validates and then you could validate that the number you are getting is what you want.

这篇关于Rails range_field 从 0 到 -100的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆