Topic: Multiple Rows of Data - Single Form - SINGLE MODEL
How can I create multiple records on a single form for a single model in Rails 3.0.3? I am NOT using nested models, so the Railscast for nested models does not apply here.
_form.html.erb
<% form_for @invoice, :html => { :multipart => true } do |f| %>
<%= f.error_messages %>
<table class="ExcelTable2007">
<tr>
<th class="heading">PO #<font color="red"> *</font></th>
<th class="heading">Invoice #<font color="red"> *</font></th>
<th class="heading">"Bill To" is Correct</th>
<th class="heading">Invoice Date<font color="red"> *</font></th>
<th class="heading">Date Received<font color="red"> *</font></th>
<th class="heading">AP Clerk Note<font color="red"> *</font></th>
<th class="heading">Invoice Attachment<font color="red"> *</font></th>
</tr>
<tr>
<td class="heading"><%= f.text_field :po_number, :size => 10 %></td>
<td class="heading"><%= f.text_field :invoice_number, :size => 10 %></td>
<td class="heading"><%= f.check_box :bill_to_address %></td>
<td class="heading"><%= f.calendar_date_select "invoice_date", :time => false, :size => 12 %></td>
<td class="heading"><%= f.calendar_date_select "date_received", :time => false, :size => 12 %></td>
<td class="heading"><%= f.text_field :clerk_note %></td>
<td class="heading"><%= f.file_field :attachment %></td>
</tr>
</p>
</table>
####### ADD A NEW INVOICE RECORD BUTTON HERE #######
<br>
<br>
<div class="actions">
<%= f.submit "Submit" %>
</div>
</p>
<% end %>invoices_controller.erb
def create
@invoice = Invoice.new(params[:invoice])
d = @invoice.date_received
respond_to do |format|
if @invoice.save
@invoice.update_attribute(:clerk_id, current_user.id)
if @invoice.created_at > d + 2.days
@invoice.update_attribute(:received_state_id, 3)
else
@invoice.update_attribute(:received_state_id, 1)
end
format.html { redirect_to(ap_open_invoices_path) }
flash.now[:notice] = "Invoice was successfully submitted."
format.xml { render :xml => @invoice, :status => :created, :location => @invoice }
email_approver(invoice)
else
format.html { render :action => "new" }
format.xml { render :xml => @invoice.errors, :status => :unprocessable_entity }
end
end
end