Attribute Accessors for Phone Number in Form


#1

hello,

I have a field in the DB called phone_number , string 10 chars. In
my form i want to make these 3 text boxes (phone1, phone2, phone3). I
am trying to figure out how to assemble this to create the phone
number before saving. I am trying the following:

In the model

before_save :make_phone

def phone1
end

def phone2
end

def phone3
end

def phone1=(p1)
end

def phone2=(p2)
end

def phone3=(p3)
end

def make_phone
self.phone_number = phone1 + phone2 + phon3
end

however i dont seem to have access to params[:model][:phone1] from
here. Any ideas on how to make this work ?

Thanks


#2

On Tue, Jun 2, 2009 at 9:59 AM, AD removed_email_address@domain.invalid wrote:

before_save :make_phone
def phone1=(p1)
end

however i dont seem to have access to params[:model][:phone1] from
here. Any ideas on how to make this work ?

Thanks

Hi, what does your param hash look like when you submit the form?

-Conrad


#3

On Jun 2, 5:59 pm, AD removed_email_address@domain.invalid wrote:

however i dont seem to have access to params[:model][:phone1] from
here. Any ideas on how to make this work ?

Your attribute writer methods don’t do anything at the moment. They’ll
have to set instance variables which can be accessed by the
before_save method. The attr_accessor shortcut can provide these.

You’re looking for something like:

before_save :make_phone
attr_accessor :phone_1, :phone_2, :phone_3

def make_phone
self.phone_number = @phone_1 + @phone_2 + @phone_3
end

-Matt


#4

Matthew MacLeod wrote:
[…]

You’re looking for something like:

before_save :make_phone
attr_accessor :phone_1, :phone_2, :phone_3

def make_phone
self.phone_number = @phone_1 + @phone_2 + @phone_3
end

-Matt

And you may be able to streamline this a bit with composed_of.

Best,

Marnen Laibow-Koser
http://www.marnen.org
removed_email_address@domain.invalid