If we are correct in assuming the @invoice object here is a result of
@invoice = Invoice.new, then another option for creating multiple,
identical invoice objects from a form row is to use some javascript. You
could write a JS function that would take the values from the initial "form
row" and duplicate them into a second form row (and so on... creating many
duplicate rows).  You would end up with a form containing data for many
Invoice objects.  In your ruby code you would need the #create method to
deal with a collection of objects rather than just a single object.


On Tue, Feb 5, 2013 at 9:35 AM, Jason King <[email protected]> wrote:

> Start with the simple case, where they create an new invoice from scratch.
>  In the `new` action (which is the action that displays the empty form to
> start with) you'll have something like @invoice = Invoice.new to
> pre-populate an @invoice object so that `form_for` in your view works.
>
> Then, once that's working, with the `create` action also working, add a
> new checkbox (or maybe a second submit button, up to you) to your form,
> something like "Create another like this one".  Then in your `create`
> action that receives your invoice you want to check for that checkbox and
> if it exists then once you've saved your invoice you're going to set @invoice
> = @invoice.dup and then render :new which will display that same form,
> this time with all the fields from the invoice that was just submitted.
>  Note that when that checkbox is checked you will need to `render` and not
> `redirect_to` - that's the only trick here really
>
> On Tue, Feb 5, 2013 at 7:59 AM, KT <[email protected]> wrote:
>
>> Correct - the issue is that I need to create the first invoice first,
>> then duplicate it as many times as needed - like an Excel row.  So, can I
>> do that all in one form or must I first save a record, then dup it?
>> Basically, the users need to create a bunch of invoices at the same time
>> and to eliminate having to enter the same info over and over, just copy the
>> row above and change a couple attributes.... just like Excel.
>>
>> I'm stuck.
>>
>>
>> On Thursday, January 31, 2013 4:48:26 PM UTC-8, Jason King wrote:
>>
>>> 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<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 sdruby+un...@**googlegroups.com.
>>>>
>>>> For more options, visit 
>>>> https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
>>>> .
>>>>
>>>>
>>>>
>>>>
>>>>  --
>>>> --
>>>> SD Ruby mailing list
>>>> [email protected]
>>>>
>>>> http://groups.google.com/**group/sdruby<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 sdruby+un...@**googlegroups.com.
>>>>
>>>> For more options, visit 
>>>> https://groups.google.com/**groups/opt_out<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.
>
>
>

-- 
-- 
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