Forum: Ruby on Rails Attribute Accessors for Phone Number in Form

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F15fdc7cb2e911b3808837f2be244add?d=identicon&s=25 AD (Guest)
on 2009-06-02 19:00
(Received via mailing list)
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
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (conradwt)
on 2009-06-02 19:31
(Received via mailing list)
On Tue, Jun 2, 2009 at 9:59 AM, AD <straightflush@gmail.com> 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
F073247b1a99fb527a9e0569c0604c1a?d=identicon&s=25 Matthew MacLeod (Guest)
on 2009-06-02 19:46
(Received via mailing list)
On Jun 2, 5:59 pm, AD <straightfl...@gmail.com> 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
Dd2d775dea75b381edb1bbf0600a0907?d=identicon&s=25 Marnen Laibow-Koser (marnen)
on 2009-06-02 23:29
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
marnen@marnen.org
This topic is locked and can not be replied to.