I have a form for a model which accepts nested attrs for several other models:
class Page < ActiveRecord::Base
belongs_to :user
has_many :images
has_many :videos
has_many :options
accepts_nested_attributes_for :images
accepts_nested_attributes_for :videos
accepts_nested_attributes_for :options
def active?
published # boolean field
end
end
I’d like the page owner (user) to be able to edit the page and its nested attrs, and see those changes immediately without needing to save the model (which would make it publicly viewable). My gut reaction is to clone the Page along with all its associations (yikes!) so that the original stays intact until the owner is satisfied with the changes to the clone.
Is there a more sensible or efficient solution?
EDIT – After looking into has_draft which is option 2 – I would 100% go with that.
You have 3 options here:
Option 1 –
You can save the data into a session and retrieve it accordingly.
Option 2 –
You can use a draft plugin/gem like “has_draft” which will clone your model structure, create a new table, etc and handle all of it for you.
https://github.com/rubiety/has_draft
Option 3 –
(not sure if this will work exactly, but it’s my best guess)
You can duplicate your page, add a new field to your page model called “duplicate_of” and when you hit your edit action, create a duplicate passing in id of the original page. Then in the update action check if it is a duplicate and if it is, overwrite the original and delete the duplicate.