# Adding multiple rows of data using a single form

**URL:** <https://www.ruby-forum.com/t/adding-multiple-rows-of-data-using-a-single-form/83306>\
**Category:** Rails\
**Created:** [January 25, 2007, 12:12am UTC](https://www.ruby-forum.com/t/adding-multiple-rows-of-data-using-a-single-form/83306 "2007-01-25T00:12:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2acd7d/32.png) [@kevin](https://www.ruby-forum.com/u/kevin)\
**Post date:** [January 25, 2007, 12:12am UTC](https://www.ruby-forum.com/t/adding-multiple-rows-of-data-using-a-single-form/83306/1 "2007-01-25T00:12:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kevin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2acd7d/32.png) [@kevin](https://www.ruby-forum.com/u/kevin)\
**Post date:** [January 25, 2007, 7:49am UTC](https://www.ruby-forum.com/t/adding-multiple-rows-of-data-using-a-single-form/83306/2 "2007-01-25T07:49:44Z")

</div>

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’) %\>  
  
\<% end %\>  
\<%= submit\_tag %\>  
\<% end %\>

should work… note the “[]” on owner…

good luck!

---

<div class="post-metadata">

**Author:** ![kevin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2acd7d/32.png) [@kevin](https://www.ruby-forum.com/u/kevin)\
**Post date:** [January 25, 2007, 9:59pm UTC](https://www.ruby-forum.com/t/adding-multiple-rows-of-data-using-a-single-form/83306/3 "2007-01-25T21:59:01Z")

</div>

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’) %\>  
>   
> \<% 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
