In my Rails app I have an invoices_controller.rb
with these actions:
def new
@invoice = current_user.invoices.build(:project_id => params[:project_id])
@invoice.build_item(current_user)
@invoice.set_number(current_user)
end
def create
@invoice = current_user.invoices.build(params[:invoice])
if @invoice.save
flash[:success] = "Invoice created."
redirect_to edit_invoice_path(@invoice)
else
render :new
end
end
Essentially, the new
method instantiates a new invoice
record plus one associated item
record.
Now, what sort of method do I need if I want to duplicate an existing invoice
?
I am a big fan of Rails's RESTful approach, so I wonder if I should add a new method like
def duplicate
end
or if I can use the existing new
method and pass in the values of the invoice
to be duplicated there?
What is the best approach and what might that method look like?