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....

2 comments
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')
end

In the view, I call this:

<%= @credential.formatted_phone %>

0
Comment actions Permalink

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

0

Please sign in to leave a comment.