Don’t know why, but cant seem to get this to work for me.
I have a phone number field for an addressbook. In the addressbook model
I have a before_update declaration that says the following:
I’d like to be able to remove all special characters (just numbers
please), but this doesn’t want to work for me.
before_update :format_phone
def format_phone
self.phone = self.phone.to_s.gsub(/\D/, “”)
end
The following works to remove all numbers, so I know it’s reaching the
before_update call. 
self.phone = self.phone.to_s.gsub(/\d/, “”)
Any help is appriciated.
Thanks!
Not sure what you’re trying to do. If the phone is represented as a
number, then doing that gsub should have no effect. If it’s a string,
you’re going to have to use:
self.phone_before_type_cast(gsub(/\D/, “”)
Right?
David C. wrote:
Don’t know why, but cant seem to get this to work for me.
I have a phone number field for an addressbook. In the addressbook model
I have a before_update declaration that says the following:
I’d like to be able to remove all special characters (just numbers
please), but this doesn’t want to work for me.
before_update :format_phone
def format_phone
self.phone = self.phone.to_s.gsub(/\D/, “”)
end
The following works to remove all numbers, so I know it’s reaching the
before_update call. 
self.phone = self.phone.to_s.gsub(/\d/, “”)
Any help is appriciated.
Thanks!
Steve R. wrote:
Not sure what you’re trying to do.
I don’t think I did either.
I realized after I read your response that I was trying to change the
wrong thing…
I was really wanting to modify the phone input param.
params[:address][:phone] = params[:address][:phone].gsub(/[\D]/, “”)
Is there a way to do the above with out the this = that?
I feel stupid.
Thanks for your help!
On May 8, 2006, at 1:37 PM, David C. wrote:
params[:address][:phone] = params[:address][:phone].gsub(/[\D]/, “”)
Is there a way to do the above with out the this = that?
I feel stupid.
Thanks for your help!
params[:address][:phone].gsub!(/[\D]/, “”)
The gsub! with an ! exclamation point modifies the receiver in place.
-Ezra