I have an Invoice to which Items can be added using some jQuery magic.
Controller:
def new
@invoice = Invoice.new
@invoice.with_blank_items(current_user)
@title = "New invoice"
end
Model:
def with_blank_items(user, n = 1)
n.times do
items.build(:price => user.preference.hourly_rate)
end
self
end
View:
<%= f.text_field number_with_precision(:price, :strip_insignificant_zeros => true) %>
Now the problem is that the price of a newly added item is always displayed in the format XX.X, i.e. with one decimal place, no matter if it is zero or not.
I don’t like that and I want a price of 50 to be displayed as 50 and not as 50.0.
Once the invoice gets saved to the database, unnecessary zeros get dropped and that’s perfect.
How can I strip insignificant zeros on newly added items as well?
You can try to format the values the right way before assigning: