ruby-on-rails – 带小数精度的Formtastic数字字段?

当然我错过了一些非常明显的东西……我有一个十进制精度为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在显示值时将其格式化.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...