Search code examples
ruby-on-railsformtastic

Formtastic number field with decimal precision?


Surely I'm missing something pretty obvious... I have a field that is decimal with precision 2, but Formtastic is displaying it with only a single decimal, unless the actual value has 2 places. What am I missing?

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

Noting answer from below, which might not be clear to others finding this later:

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

Solution

  • Try this:

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

    Sales_price is being stored in your database with two decimal places, but you have to tell rails to format it that way when displaying the value.