bigdecimal - Can't get decimals to display in text field for Rails 3 -
confused rails 3 newbie. model has purchase_price attribute defined this:
t.decimal :purchase_price, :precision => 12, :scale => 2, :default => 0.00
my goal have simple rails 3 app can view , edit value. in model, defined custom getter force 2 decimal places exist.
def purchase_price sprintf( "%.2f",attributes['purchase_price']) end
this works correctly console , when display show page. on edit page have text field edit value. problem if value saved '123.00' value displayed '123.0'.
questions:
- why custom getter not getting called?
- why displayed 1 decimal , not 2 decimals?
update
- decimal value stored in mysql: 100.20
- value returned console: 100.20
- value displayed in text field: 100.2
- value returned via firebug: "100.2"
- the text field length not issue.
- if value in db 100.21, displayed correctly 100.21
try this
f.text_field :purchase_price, :value=>number_to_currency(f.object.purchase_price, :unit=>'')
that should convert number currency (with 2 decimal places) , :unit=>''
exclude "$".
and i'd recommend removing custom purchase_price
method , using number_to_currency
helper needed instead. overriding purchase_price
accessor isn't bad idea confuse things might expecting number rather string.
Comments
Post a Comment