I''m retrieving information from the database through an ActiveRecord model and displaying it in my view (basic stuff). I just want to simply be able to format some of my attributes that I''m displaying in my view. For example, my postal code is stored as a 9 digit number in the database. How can I format the output so that it will add a dash in the center, displaying "12345-6789". I also want to be able to format my phone number output. I know how I''d do this in PERL, but I can''t seem to figure this out in RoR. Thanks in advance for any help! -- 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 -~----------~----~----~----~------~----~------~--~---
Raja Venkataraman
2006-Oct-20 03:53 UTC
Re: Simple formatting of an ActiveRecord Attribute
John wrote:> I''m retrieving information from the database through an ActiveRecord > model and displaying it in my view (basic stuff). I just want to simply > be able to format some of my attributes that I''m displaying in my view. > For example, my postal code is stored as a 9 digit number in the > database. How can I format the output so that it will add a dash in the > center, displaying "12345-6789". I also want to be able to format my > phone number output. I know how I''d do this in PERL, but I can''t seem to > figure this out in RoR. Thanks in advance for any help!You could have helper methods defined that do this conversion for you. Something like def format_zipcode(code) if code.length > 5 code.insert(5, "-") end end and call the helper methods from the view. I was searching for a "after_" method in the activerecord that I could have in my model so that when the field is retrieved, it would be modified as per my needs but couldnt find one. -- 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 -~----------~----~----~----~------~----~------~--~---