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
On Tue, Jun 2, 2009 at 9:59 AM, AD <straightflush-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 >Hi, what does your param hash look like when you submit the form? -Conrad> > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jun 2, 5:59 pm, AD <straightfl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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
Marnen Laibow-Koser
2009-Jun-02 21:29 UTC
Re: Attribute Accessors for Phone Number in Form
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 > > -MattAnd you may be able to streamline this a bit with composed_of. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.