当然,我遗漏了一些非常明显的东西......我有一个精度为 2 的小数字段,但 Formtastic 仅以一位小数显示它,除非实际值有 2 位。我缺少什么?
Model:
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
View
%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, input_html: { class: 'span2', value: number_with_precision(bought.sales_price, precision: 2) }, label: false
Sales_price 以两位小数存储在数据库中,但是您必须告诉 Rails 在显示值时以这种方式格式化它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)