Topic: Multiple rows of data with a single form
I've been trying to configure a submit form where users can add multiple names to a database at once, instead of individually.
So far, here's the code I have for my view:
<% 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 %>
While this properly generates the form, I seem to be having a problem with the controller. Originally, here's what I had in my controller:
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
This resulted in an error saying "undefined method `1=' for #<Owner:0x260f4cc>". I played around a bit and changed the first line of the action to this:
params[:owner].each_value { |i| @owner = Owner.new(i) }However, this just submits a single row of data and ignores the rest.
Any advice would be greatly appreciated. Thanks.