How can I format the value shown in a Rails edit field?

The best I have come up with so far is something like this:

<%= f.text_field :my_attribute, :value => number_with_precision(f.object.my_attribute) %>

Or my_attribute could return the formatted value, like this:

def my_attribute
  ApplicationController.helpers.number_with_precision(read_attribute(:my_attribute))
end

But you still have to use :value

<%= f.text_field :my_attribute, :value => f.object.my_attribute %>

This seems like a lot of work.

Leave a Comment