ruby-on-rails – 带小数精度的Formtastic数字字段?
发布时间:2020-12-17 03:13:50 所属栏目:百科 来源:网络整理
导读:当然我错过了一些非常明显的东西……我有一个十进制精度为2的字段,但Formtastic只显示一个小数,除非实际值有2个位置.我错过了什么? 模型: create_table "items",:force = true do |t| t.string "item_number" t.integer "buyer_id" t.integer "seller_id"
当然我错过了一些非常明显的东西……我有一个十进制精度为2的字段,但Formtastic只显示一个小数,除非实际值有2个位置.我错过了什么?
模型: create_table "items",:force => true do |t| t.string "item_number" t.integer "buyer_id" t.integer "seller_id" t.string "description" t.decimal "sales_price",:precision => 10,:scale => 2,:default => 0.0 t.datetime "created_at" t.datetime "updated_at" end 视图 %td= bought.input :sales_price,input_html: { class: 'span2'},label: false 注意到下面的回答,其他人在以后发现这个问题可能并不清楚: %td= bought.input :sales_price,input_html: { class: 'span2',value: number_with_precision(bought.object.sales_price,precision: 2)},label: false 解决方法
试试这个:
%td= bought.input :sales_price,value: number_with_precision(bought.sales_price,precision: 2) },label: false Sales_price存储在数据库中,带有两个小数位,但您必须告诉rails在显示值时将其格式化. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |