I have a form where I want users to be able to add multiple names into a database at once, instead of individually. Currently my form is setup similar to this: <% form_for(:owner, :url => owner_path) do |f| %> First Name: <%= f.text_field :firstname %> Last Name: <%= f.text_field :lastname %> First Name: <%= f.text_field :firstname %> Last Name: <%= f.text_field :lastname %> First Name: <%= f.text_field :firstname %> Last Name: <%= f.text_field :lastname %> <% end %> However, as expected this just adds the data from the first set of text fields and ignores the others. Any pointers on how I can allow the addition of multiple names at once? Thanks. -- 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 -~----------~----~----~----~------~----~------~--~---
mcintyre.tim-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jan-25 06:49 UTC
Re: Adding multiple rows of data using a single form
Not sure of an elegant way to do it with form_for but perhaps this will give you some ideas... <% form_tag do %> <% for @owner in @owners %> <%= text_field("owner[]" , ''firstname'') %><br /> <% end %> <%= submit_tag %> <% end %> should work... note the "[]" on owner... good luck! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
unknown wrote:> Not sure of an elegant way to do it with form_for but perhaps this will > give you some ideas... > > <% form_tag do %> > <% for @owner in @owners %> > <%= text_field("owner[]" , ''firstname'') %><br /> > <% end %> > <%= submit_tag %> > <% end %> > > should work... note the "[]" on owner... > > good luck!Thanks. I did find a way using form_for: <% form_for(:owner, :url => owner_path) do |f| %> <% (1..3).each do |i| -%> First Name: <%= f.text_field :firstname, :index => i %> Last Name: <%= f.text_field :lastname, :index => i %> <% end -%> <%= submit_tag "Add Names" %> <% end %> However, I now apparently need to make some changes to my controller for this to work. Here''s the current create action: def create @owner = Owner.new(params[:owner]) if @owner.save flash[:notice] = ''Owner was succesfully added.'' redirect_to :action => ''unpaid'' else render :action => ''new'' end end When submitting data, I get the following error: NoMethodError in OwnersController#create undefined method `1='' for #<Owner:0x260f4cc> -- 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 -~----------~----~----~----~------~----~------~--~---