mcintyre.tim-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Apr-18 23:36 UTC
form.text_field display of sql decimal
hey all, I''ve got this column in a mysql table: wholesale_price decimal(6,2) this is the actual value that is currently stored in the db: +-----------------+ | wholesale_price | +-----------------+ | 19.00 | +-----------------+ the the view code to edit that: <%= form.text_field :wholesale_price %> but then the rendered html source looks like this: <input id="film_wholesale_price" name="film[wholesale_price]" size="5" type="text" value="19.0" /> when I want it to look like this: <input id="film_wholesale_price" name="film[wholesale_price]" size="5" type="text" value="19.00" /> <-- note the extra "0" I mucked around with creating a helper method to fix that up but seem to be missing something simple/obvious. Thanks in advance for any help! Tim --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> but then the rendered html source looks like this: > <input id="film_wholesale_price" name="film[wholesale_price]" size="5" > type="text" value="19.0" /> > > when I want it to look like this: > <input id="film_wholesale_price" name="film[wholesale_price]" size="5" > type="text" value="19.00" /> <-- note the extra "0"imho its better to use textfield in the db, and store price data in the ''Money'' type (off course, you must serialize this field in your model). don''t forget to install money.gem and `require` it. -- wbr, sh0ckfile. FileForum moders team http://www.fforum.ru --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
That''s a good approach. When I first started using Rails, I found this problem and it bugged me so I tried to write a plugin to override this behavior. I got it working but it was way more trouble than it was worth. My approach was to add two new methods to my model. #f retreives the wholesale price and formats it as a decimal def format_wholesale_price sprintf("%01.2f", self.wholesale_price end # Sets the value (needed to make the form helper work def format_wholesale_price=(value) self.wholesale_price = value end Then just use those in your helpers. <%= form.text_field :format_wholesale_price %> That should work. On 4/18/07, Sh0Ck_filE <sh0ckfile-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > but then the rendered html source looks like this: > > <input id="film_wholesale_price" name="film[wholesale_price]" size="5" > > type="text" value="19.0" /> > > > > when I want it to look like this: > > <input id="film_wholesale_price" name="film[wholesale_price]" size="5" > > type="text" value="19.00" /> <-- note the extra "0" > > imho its better to use textfield in the db, and store price data in the > ''Money'' type (off course, you must serialize this field in your model). > > don''t forget to install money.gem and `require` it. > > -- > wbr, sh0ckfile. > FileForum moders team > http://www.fforum.ru > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---