带小数精度的Formtastic数字字段?

Bre*_*nda 5 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
Run Code Online (Sandbox Code Playgroud)

视图

%td= bought.input :sales_price, input_html: { class: 'span2'}, label: false
Run Code Online (Sandbox Code Playgroud)

注意到下面的回答,其他人在以后发现这个问题可能并不清楚:

%td= bought.input :sales_price, input_html: { class: 'span2', value: number_with_precision(bought.object.sales_price, precision: 2)}, label: false
Run Code Online (Sandbox Code Playgroud)

Nic*_*gan 5

尝试这个:

%td= bought.input :sales_price, input_html: { class: 'span2', value: number_with_precision(bought.sales_price, precision: 2) }, label: false
Run Code Online (Sandbox Code Playgroud)

Sales_price 以两位小数存储在您的数据库中,但您必须告诉 rails 在显示值时以这种方式对其进行格式化。