my phones table is containing a contact_id field ... (phone model) belongs_to :contact (contact model) has_many :phones validates_associated :phones in my contact creation form i added that field: <% text_field ''phone'', ''number'' %> and in the create action of the contact controller looks like this def create @contact = Contact.new(params[:contact]) @phone = @contact.phones.create @phone.attributes = params[:phone] if @contact.save flash[:notice] = ''Contact was successfully created.'' redirect_to :action => ''list'' else render :action => ''new'' end end and it works great....it even highlight the phone number in case of validation fails because of the validates_associated inside the phone model... my problem? How can i add multiple phones???? i tried: <% text_field ''phone'', ''number'' , :index => 1 %> <% text_field ''phone'', ''number'' , :index => 2 %> etc... that gave me that error: undefined method `1='' for #<Phone:0x221b968> WHAT CAN I DO ???? HELP?????????
I don''t have the answer you are looking for, but wondering if you used an ajax form for this? I believe we talked about this last week. I was looking for a way to use Ajax to check to see if the password is long enough (for example) and if not use the highlight effect to highlight the message. But if so, redirect to a success page. Thanks, Steve On 8/7/05, olivier Hericord <olivier.hericord.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> my phones table is containing a contact_id field ... > > (phone model) > belongs_to :contact > > (contact model) > has_many :phones > validates_associated :phones > > > in my contact creation form i added that field: > <% text_field ''phone'', ''number'' %> > and in the create action of the contact controller looks like this > > def create > @contact = Contact.new(params[:contact]) > @phone = @contact.phones.create > @phone.attributes = params[:phone] > if @contact.save > flash[:notice] = ''Contact was successfully created.'' > redirect_to :action => ''list'' > else > render :action => ''new'' > end > end > > and it works great....it even highlight the phone number in case of > validation fails because of the validates_associated inside the phone > model... > > my problem? > > How can i add multiple phones???? > > i tried: > <% text_field ''phone'', ''number'' , :index => 1 %> > <% text_field ''phone'', ''number'' , :index => 2 %> > etc... > > that gave me that error: > undefined method `1='' for #<Phone:0x221b968> > > > WHAT CAN I DO ???? > HELP????????? > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
in the contact view to create a new contact , a textfield named phone[number] is recognized as a phone and is validated via the validates_associated....in case of validation fails the textfield is highlighted as expected.... but my relation is contact has_many :phones ... so i want at least 2 or 3 phones for each created contact, so i need to have 2 or 3 textfield i tried name=phone[number][] , itried name=phone[number][1], itried name=phone[1][number], itried name=phone[]number]....nothing seems to work.... <% text_field ''phone'', ''number'' , :index => 1 %> gave me that error: undefined method `1='' for #<Phone:0x221b968> what''s the method ? HELP PLEASE....stuck on that for days thanks On 8/8/05, Steve Odom <steve.odom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I don''t have the answer you are looking for, but wondering if you used > an ajax form for this? I believe we talked about this last week. I was > looking for a way to use Ajax to check to see if the password is long > enough (for example) and if not use the highlight effect to highlight > the message. But if so, redirect to a success page. > > Thanks, > > Steve > > On 8/7/05, olivier Hericord <olivier.hericord.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > my phones table is containing a contact_id field ... > > > > (phone model) > > belongs_to :contact > > > > (contact model) > > has_many :phones > > validates_associated :phones > > > > > > in my contact creation form i added that field: > > <% text_field ''phone'', ''number'' %> > > and in the create action of the contact controller looks like this > > > > def create > > @contact = Contact.new(params[:contact]) > > @phone = @contact.phones.create > > @phone.attributes = params[:phone] > > if @contact.save > > flash[:notice] = ''Contact was successfully created.'' > > redirect_to :action => ''list'' > > else > > render :action => ''new'' > > end > > end > > > > and it works great....it even highlight the phone number in case of > > validation fails because of the validates_associated inside the phone > > model... > > > > my problem? > > > > How can i add multiple phones???? > > > > i tried: > > <% text_field ''phone'', ''number'' , :index => 1 %> > > <% text_field ''phone'', ''number'' , :index => 2 %> > > etc... > > > > that gave me that error: > > undefined method `1='' for #<Phone:0x221b968> > > > > > > WHAT CAN I DO ???? > > HELP????????? > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 8/8/05, olivier Hericord <olivier.hericord.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> in the contact view to create a new contact , a textfield named > phone[number] is recognized as a phone and is validated via the > validates_associated....in case of validation fails the textfield is > highlighted as expected.... > > but my relation is contact has_many :phones ... so i want at least 2 or 3 > phones for each created contact, so i need to have 2 or 3 textfieldI''m having the exact same problem. My current thinking is to code up some Javascript that will add the additional form elements to the page. Easy enough but the problem will be dealing with them in the controller. Again: not the answer you''re looking for but I''d be very interested in any pointers anyone can provide. I''m assuming with your model if the phone number does not exist in the phone table then you''ll add it in the contacts controller when the form is submitted rather than throw an exception. <snip> F.
i tried that way....i made an ajax form to add textfield on the fly....but in case of validation error the problem remains: the corresponding textfield (ajax created or not) is not highlighted..... in a contact view <% text_field ''phone'', ''number'' %> just allows to define one phone....HOW CAN I DEFINE MORE? On 8/8/05, Felix Ingram <f.ingram.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 8/8/05, olivier Hericord <olivier.hericord.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > in the contact view to create a new contact , a textfield named > > phone[number] is recognized as a phone and is validated via the > > validates_associated....in case of validation fails the textfield is > > highlighted as expected.... > > > > but my relation is contact has_many :phones ... so i want at least 2 or 3 > > phones for each created contact, so i need to have 2 or 3 textfield > > I''m having the exact same problem. My current thinking is to code up > some Javascript that will add the additional form elements to the > page. Easy enough but the problem will be dealing with them in the > controller. > > Again: not the answer you''re looking for but I''d be very interested in > any pointers anyone can provide. I''m assuming with your model if the > phone number does not exist in the phone table then you''ll add it in > the contacts controller when the form is submitted rather than throw > an exception. > > <snip> > > F. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >