How can I format the value shown in a Rails edit field?

后端 未结 6 1969
别跟我提以往
别跟我提以往 2020-12-01 07:56

I would like to make editing form fields as user-friendly as possible. For example, for numeric values, I would like the field to be displayed with commas (like number

6条回答
  •  悲哀的现实
    2020-12-01 08:27

    This is an old question, but in case anyone comes across this you could use the number_to_X helpers. They have all of the attributes you could ever want for displaying your edit value:

    <%= f.text_field :my_number, :value => number_to_human(f.object.my_number, :separator => '', :unit => '', :delimiter => '', :precision => 0) %>
    

    There are more attributes available too: http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html

提交回复
热议问题