Gsub no workie


#1

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

self.phone = self.phone.to_s.gsub(/\d/, “”)

Any help is appriciated.

Thanks!


#2

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

self.phone = self.phone.to_s.gsub(/\d/, “”)

Any help is appriciated.

Thanks!


#3

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!


#4

The gsub! with an ! exclamation point modifies the receiver in place.

Rockin! Thanks!


#5

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