Raji Mani
2008-Nov-11 09:46 UTC
how to retrive multiple vaules from a view for a text_field!
Hi, I have view page which takes all the information like contact''s emails , phones no''s etc and saves different tables. I want to save more than one email id''s for a particular contact. view page is something like this.. . . Email 1 : <%= select ''email'', ''email_type'', %w(Business Personal), :include_blank => false %> <%= text_field ''email'',''email'',:size=>"35",:maxlength=>"80" %> <%= radio_button ''email'', ''isprimary'', ''1'' %>Primary Email 2 : <%= select ''email'', ''email_type'', %w(Business Personal), :include_blank => false %> <%= text_field ''email'',''email'',:size=>"35",:maxlength=>"80" %> <%= radio_button ''email'', ''isprimary'', ''1'' %>Primary . . Controller is : def create @contact = Contact.new(params[:contact]) @contact.emails << Email.new(params[:email]) @contact.phones << Phone.new(params[:phone]) @contact.addresses << Address.new(params[:address]) puts "hellooooooo bharati" params.inspect if @contact.save flash[:notice] = "Contact was successfully created." redirect_to :action => ''list'' else render :action => ''new'' flash[:notice] = "Contact was not created." end end Can any one help me how to send more than one email ids to controller.. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Nov-11 10:19 UTC
Re: how to retrive multiple vaules from a view for a text_field!
On Nov 11, 9:46 am, Raji Mani <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, > > I have view page which takes all the information like contact''s emails , > phones no''s etc and saves different tables. I want to save more than one > email id''s for a particular contact. > > view page is something like this.. >There''s two separate problems here: - how you manage the ui, ie adding extra textfields to the page - how you convince rails to treat these textfields as an array. The key to the second part is how you name the parameters. In particular parameters ending with a [] are treated as arrays. Fred> . > . > Email 1 : > <%= select ''email'', ''email_type'', %w(Business Personal), :include_blank > => false %> > <%= text_field ''email'',''email'',:size=>"35",:maxlength=>"80" %> > <%= radio_button ''email'', ''isprimary'', ''1'' %>Primary > Email 2 : > <%= select ''email'', ''email_type'', %w(Business Personal), :include_blank > => false %> > <%= text_field ''email'',''email'',:size=>"35",:maxlength=>"80" %> > <%= radio_button ''email'', ''isprimary'', ''1'' %>Primary > . > . > > Controller is : > > def create > @contact = Contact.new(params[:contact]) > @contact.emails << Email.new(params[:email]) > @contact.phones << Phone.new(params[:phone]) > @contact.addresses << Address.new(params[:address]) > puts "hellooooooo bharati" > params.inspect > if @contact.save > flash[:notice] = "Contact was successfully created." > redirect_to :action => ''list'' > else > render :action => ''new'' > flash[:notice] = "Contact was not created." > end > end > > Can any one help me how to send more than one email ids to controller.. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Raji Mani
2008-Nov-12 05:18 UTC
Re: how to retrive multiple vaules from a view for a text_field!
Frederick Cheung wrote:> On Nov 11, 9:46�am, Raji Mani <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> Hi, >> >> I have view page which takes all the information like contact''s emails , >> phones no''s etc and saves different tables. I want to save more than one >> email id''s for a particular contact. >> >> view page is something like this.. >> > There''s two separate problems here: > - how you manage the ui, ie adding extra textfields to the page > - how you convince rails to treat these textfields as an array. > > The key to the second part is how you name the parameters. In > particular parameters ending with a [] are treated as arrays. > > FredYes . I was able to submit more than one text field values.. but now my issue is how do i differentiate radio buttons with the same name.. both the radio buttons are getting selected :(.. here is the code <% form_for :@contact do |f| %> <% @contact.emails.each_with_index do |email, index| %> <% fields_for "email[#{index}]", email do |f| %><tr> <td> Email   <%= f.select :email_type, %w(Business Personal), :include_blank => false %> <%= f.text_field :email %> <%= f.radio_button :isprimary ,''index'' %>Primary </td></tr> <% end %> <% end %> <% end %> -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Nov-12 09:31 UTC
Re: how to retrive multiple vaules from a view for a text_field!
On Nov 12, 5:18 am, Raji Mani <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Frederick Cheung wrote: > > On Nov 11, 9:46 am, Raji Mani <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > wrote: > >> Hi, > > >> I have view page which takes all the information like contact''s emails , > >> phones no''s etc and saves different tables. I want to save more than one > >> email id''s for a particular contact. > > >> view page is something like this.. > > > There''s two separate problems here: > > - how you manage the ui, ie adding extra textfields to the page > > - how you convince rails to treat these textfields as an array. > > > The key to the second part is how you name the parameters. In > > particular parameters ending with a [] are treated as arrays. > > > Fred > > Yes . I was able to submit more than one text field values.. but now my > issue is how do i differentiate radio buttons with the same name.. both > the radio buttons are getting selected :(.. here is the code >the builders yielded by form_for and fields_for have the same name, so they''ll be overwriting each other (which isn''t what I thing you want). Also shouldn''t the second parameter to radio_button be index and not ''index'' ? Fred> <% form_for :@contact do |f| %> > <% @contact.emails.each_with_index do |email, index| %> > <% fields_for "email[#{index}]", email do |f| %><tr> <td> > Email   > <%= f.select :email_type, %w(Business Personal), > :include_blank => false %> > <%= f.text_field :email %> > <%= f.radio_button :isprimary ,''index'' %>Primary > </td></tr> > <% end %> > <% end %> > <% end %> > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Raji Mani
2008-Nov-12 10:13 UTC
Re: how to retrive multiple vaules from a view for a text_field!
Frederick Cheung wrote:> On Nov 12, 5:18�am, Raji Mani <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> >> issue is how do i differentiate radio buttons with the same name.. both >> the radio buttons are getting selected :(.. here is the code >> > the builders yielded by form_for and fields_for have the same name, so > they''ll be overwriting each other (which isn''t what I thing you want). > Also shouldn''t the second parameter to radio_button be index and not > ''index'' ? > > Fredya. got it. But there exists an issues.. When i select radio button i ll get the following error. . "undefined method `values'' for "1":String" where as the other values get saved if i do not select radio button. Also If i enter only one email id the form is not getting saved. Controller is as follows. def new @contact = Contact.new 2.times { @contact.emails.build } end def create @contact = Contact.new(params[:contact]) params[:email].each_value do |email| @contact.emails.build(email) unless email.values.all?(&:blank?) end if @contact.save flash[:notice] = "Successfully created new Contact.!!!!!!!!!" redirect_to :action => ''list'' else render :action => ''new'' end end Where i went wrong? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Nov-12 10:32 UTC
Re: how to retrive multiple vaules from a view for a text_field!
On 12 Nov 2008, at 10:13, Raji Mani wrote:> > Frederick Cheung wrote: >> On Nov 12, 5:18�am, Raji Mani <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> >> wrote: >>> >>> issue is how do i differentiate radio buttons with the same name.. >>> both >>> the radio buttons are getting selected :(.. here is the code >>> >> the builders yielded by form_for and fields_for have the same name, >> so >> they''ll be overwriting each other (which isn''t what I thing you >> want). >> Also shouldn''t the second parameter to radio_button be index and not >> ''index'' ? >> >> Fred > > ya. got it. But there exists an issues.. When i select radio button > i ll > get the following error. . "undefined method `values'' for "1":String" > where as the other values get saved if i do not select radio button. >From the code I''d guess that you end up with params[:email] containing the string 1, which you then try and call values on. Fred> Also If i enter only one email id the form is not getting saved. > Controller is as follows. > > def new > @contact = Contact.new > 2.times { @contact.emails.build } > end > def create > @contact = Contact.new(params[:contact]) > params[:email].each_value do |email| > @contact.emails.build(email) unless email.values.all?(&:blank?) > end > if @contact.save > flash[:notice] = "Successfully created new Contact.!!!!!!!!!" > redirect_to :action => ''list'' > else > render :action => ''new'' > end > end > > Where i went wrong? > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---