My understanding is that that's what ActiveRecord::Base#dup does.

Based on the error you're getting KT I'd actually guess that the problem
here is that the initial @invoice that's used in the form_for doesn't have
an id, which means that Invoice.find(params[:id]) is being called with a
nil id, which generates the "Couldn't find an invoice without an ID" error
that you're getting.

So, how are you setting that @invoice in the controller action that *
displays* the form?


On Thu, Jan 31, 2013 at 3:42 PM, Robert Kaufman <[email protected]> wrote:

> Hi KT,
>   The problem is that Rails automatically decides whether you are updating
> or creating a new record based on whether your instance (@invoice in this
> case) things it is a new record or not.  The memory dup you are making
> thinks it is an existing record, even though its id is blank.  Changing
> your controller action to something like this should do the trick:
>
> def clone_invoice
>   invoice = Invoice.find(params[:id])
>   new_invoice = Invoice.new(invoice.attributes.merge('id' => nil))
> end
>
>
> Note, I removed your @ variables, since we're really only using them
> inside of this method.  It would be even better to have a make_clone method
> in your model which you would just call on the original invoice object
> (that's just a little more OO).
>
> Best,
> Rob
>
> On Jan 31, 2013, at 2:05 PM, KT <[email protected]> wrote:
>
> Hi all!  I was hoping someone could help me with another issue...
>
> I have an INVOICE form.  Users would like to create multiple records at
> the same time.  In addition, they would like to copy the first record's
> attributes for the next record, so they only have to change a few fields.
> Less typing.  MS Excel style.
>
> I've seen the Railscast for adding multiple records through nested
> attributes, but I'm using a single model, so I don't know how that
> applies.  I do not want to break out my flat invoice table if I can avoid
> it.
>
> I've tried the following, but when I click on "Add Invoice", I get an
> error: "Couldn't find an invoice without an ID".
>
> _form.html.erb:
>
> <% form_for @invoice, :html => { :multipart => true } do |f| %>
>
> <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">Division<font color="red"> *</font></th>
>        <th class="heading">GR Approver<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.collection_select :division_id,
> Division.active.order(:division), :id, :division, :include_blank => true
> %></td>
>        <td class="heading"><%= f.collection_select :approver_username,
> Aduser.order(:last_name, :first_name), :username, :fullname, :include_blank
> => true %></td>
>        <td class="heading"><%= f.file_field :attachment %></td>
>       </tr>
>     </p>
> </table>
> &emsp;&emsp;&emsp;&emsp;<%= link_to "Add Invoice",
> clone_invoice_url(@invoice) %>
>   <br>
>   <br>
>
>   <div class="actions">
>     <%= f.submit "Submit" %>
>   </div>
>   </p>
> <% end %>
>
> ---------------------------------------------
>
> invoices_controller:
>
> def clone_invoice
>   @invoice = Invoice.find(params[:id])
>   @new_invoice = @invoice.dup
> end
>
> --
> --
> SD Ruby mailing list
> [email protected]
> http://groups.google.com/group/sdruby
> ---
> You received this message because you are subscribed to the Google Groups
> "SD Ruby" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
>
>  --
> --
> SD Ruby mailing list
> [email protected]
> http://groups.google.com/group/sdruby
> ---
> You received this message because you are subscribed to the Google Groups
> "SD Ruby" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
--- 
You received this message because you are subscribed to the Google Groups "SD 
Ruby" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to