I have a nested form and once I save, I want to be able to click a link on the show page to copy or clone that form and open a new one. From there I should be able to make edits (like a new id) and save as a new record. I have seen some examples like this deep_cloneable gem, but I have no idea how to implement it. I think this should be simple, but I just don't understand where to put things in the controller and in the show view.
相关问题
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- Removing duplicate dataframes in a list
- Eager-loading association count with Arel (Rails 3
- Django distinct is not working
相关文章
- Right way to deploy Rails + Puma + Postgres app to
- AWS S3 in rails - how to set the s3_signature_vers
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
- “No explicit conversion of Symbol into String” for
- form_for wrong number of arguments in rails 4
- Rspec controller error expecting <“index”> but
- Factory_girl has_one relation with validates_prese
If you want to copy an activeRecord object you can use its attributes to create new one like
you can have an action in your controller which can be called on link,
I found these answers a little hard to follow. One answer shows this:
which will not work as it will also pass the id, and timestamp values. I used .dup to fix that and I show that in my answer.
Here's how I achieved creating a new item from an existing item.
The model is for a Product, the controller Products_Controller.rb. We're going to add a new action to the controller called COPY and we're going to link to it from the SHOW view on an existing Product and render a filled out NEW view ready to be edited and saved.
First we create a route for the copy action in routes.rb
Then a copy action in Products_controller.rb
Now we need to add a Link to the SHOW view to call our copy action.
This worked for me. I hope it works for you and that the answer is simple enough to follow.
Also worth mentioning is the
dup
method on a model. It makes a copy with all attributes and outgoing relations but setsid
tonil
. Like this (borrowing code from Naren Sisodiya):