Howdy –
I have a price field in my db, :decimal, :null => false, :default =>
0
When I open a new form field to create a new record, this value is
displayed as “0.0”. This is not so pretty for a field in which a
price like “123.45” is expected. Ideally I would like to have the
field blank (I’ll validate for numeric at submit time). I’m aware of
the format_as_currency helper, but I am not sure how/if I would use it
in a form context.
Bonus question: I am currently validating acceptable characters in a
javascript onkeyup event (get rid of anything not a digit, decimal or
comma). Is there anything that comes for free that I have missed, or
a good plugin out there to help with what I would think is a fairly
common use case?
Obviously, I’m a rails noob
Regards, and thanks!
Tom
Well, then you need to remove :null => false and :default => 0 from
migration.
and then just do the validation
On Oct 13, 4:28 pm, Jamal S. [email protected]
wrote:
Well, then you need to remove :null => false and :default => 0 from
migration.
and then just do the validation
Posted viahttp://www.ruby-forum.com/.
Thanks. Yes, your solution will fix the display, but…
The migration defines both the type of the object within the Rails
context, but also the actual database field definition, right? If I
want to have very basic data integrity, using not null, defaults (not
to mention explicit foreign key constraints) is a “best practice”
which I am pretty sure is still in force in the Rails world.
Any other ideas on how I can change the display of my data without
changing the migration and database column definitions?
Thanks!!
Tom
Any other ideas on how I can change the display of my data without
changing the migration and database column definitions?
you could try overriding the getter in your model:
assuming the field is ‘my_number’:
def my_number
return nil if self[:my_number].blank? || self[:my_number] == 0
self[:my_number]
end