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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jamal Soueidan
2007-Oct-13 20:28 UTC
Re: Form field --display :decimal 0.0 as 0.00, or blank?
Well, then you need to remove :null => false and :default => 0 from migration. and then just do the validation :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Oct 13, 4:28 pm, Jamal Soueidan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---