Simple things like duplicating a record in rails?


#1

Maybe I am totally waisted after a long nights work with my admin
interface filters but how the heck can I duplicate records?

Scenario:
My client will enter hundreds maybe thousands of products into their
new nice e-commerce system backoffice forms. They call me and say,
Hey! Can we press a “DUPLICATE” button would safe us hours and hours
of time when adding products.

Solution?
I can’t find any posts about this issues on the lists archives or
anywhere else for that matter. I just want to have in my scaffolds
next to show, edit, destroy a new link with the label “Duplicate and
Edit” and if you press this link it should take the data on the
selected product, copy it to a new product and go to the edit screen
of that new product.

Any ideas on how to accomplish this?


#2

Andreas Kviby wrote:

I can’t find any posts about this issues on the lists archives or
anywhere else for that matter. I just want to have in my scaffolds
next to show, edit, destroy a new link with the label “Duplicate and
Edit” and if you press this link it should take the data on the
selected product, copy it to a new product and go to the edit screen
of that new product.

Any ideas on how to accomplish this?

How about object.clone?


Cheers,

  • Jacob A.

#3

#controller
def duplicate_and_edit
@product = Product.find(params[:id]
@product.id = nil
end

if your form in new.rhtml is populated with form_for @product … ,
you could just render this action and rails will fill in the values
into the form and thus reuse this view instead a creating a duplicate
form for this action.


#4

Hi
Thanks a lot, it worked like a charm. Thanks to you guys my client
will be very happy today. /Andreas

Read my blog at www.swerails.se