Rails number_field alternative for decimal values

前端 未结 2 1214
时光取名叫无心
时光取名叫无心 2020-12-07 21:34

I\'m trying to accept a decimal value (USD, so 12.24 would be an example) with the number_field method.

<%= f.number_fiel
2条回答
  •  一整个雨季
    2020-12-07 22:17

    You can bypass the "only Integers" constraint by adding a Float for the step option:

    f.number_field :amount, step: 0.5
    

    Update: Actually you can use the value 'any' for the step, it will accept all floats and integers, and the step will be 1:

    f.number_field :amount, step: :any
    

    Update for prices:

    You can use the rails' helper number_to_currency to display a price inside a number_field:

    f.number_field :amount, value: number_to_currency(f.object.amount.to_f, delimiter: '', unit: ''), step: :any
    

提交回复
热议问题