Simple formatting of an ActiveRecord Attribute

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!

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs