Create telephone mask

In the database, the telephone field is stored as a string of numbers.  However, I want to present it in the view as (999) 999-9999.  I came up with a regular expression with gsub.  It works in the console but not in the view.

Here is the example:

I had to put this in the controller to make this work.  But I'd rather it be in the model or the view, whichever is best.  This information is going to be read only.

@credential.Telephone.gsub!(/^(\d{3})(\d{3})(\d{4})/,'(\1) \2-\3')

Then in the view I just call

<%h= @credential.Telephone %> # if I use gsub here instead of the controller it doesn't work

How can I make this work in th emodel or view?

And sorry if this is a non-IDE question.  Is this forum only for IDE-related questions?

Thanks in advance....

Comment actions Permalink

I got this to work:

In the model I did this:

def formatted_phone
      self.Telephone.gsub(/^(\d{3})(\d{3})(\d{4})$/,'(\1) \2-\3')

In the view, I call this:

<%= @credential.formatted_phone %>

Comment actions Permalink

This forum is used for the RubyMine related questions. For generic Ruby/Rails questions please use some other forum or


Please sign in to leave a comment.