Rails range_field from 0 to -100

做~自己de王妃 提交于 2019-12-24 14:11:18

问题


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

Here's my current code:

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

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?

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?


回答1:


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.



来源:https://stackoverflow.com/questions/15313892/rails-range-field-from-0-to-100

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!