[英]Rails 3: Privately Preview Model Changes
我有一個模型的表單,該表單接受其他幾個模型的嵌套屬性:
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
我希望頁面所有者(用戶)能夠編輯頁面及其嵌套的attrs,並立即查看那些更改,而無需保存模型(這將使其公開可見)。 我的直覺反應是克隆Page及其所有關聯(喜歡!),以便原始文檔保持原樣,直到所有者對克隆的更改滿意為止。
有沒有更明智或更有效的解決方案?
編輯-研究了選項2的has_draft之后-我會100%同意。
您在這里有3個選擇:
選項1 -
您可以將數據保存到會話中並進行相應的檢索。
選項2-
您可以使用草案插件/ gem之類的“ has_draft”,它將克隆您的模型結構,創建新表等,並為您處理所有這些。
https://github.com/rubiety/has_draft
選項3-
(不確定這是否會正常工作,但這是我的最佳猜測)
您可以復制頁面,在頁面模型中添加一個名為“ duplicate_of”的新字段,然后在您執行編輯操作時,創建一個原始頁面ID的副本傳遞。 然后在更新操作中檢查它是否是重復項,如果是,請覆蓋原始副本並刪除重復項。
def edit
@original = User.find(params[:user_id]).pages.find(params[:id])
@clone.duplicate_of = @original.id
@clone.active = false
@page = User.find(params[:user_id]).pages.create(@clone.attributes)
end
def update
#you will need to add something here to check if its a duplicate
#to begin with and if it is...
@page = User.find(params[:user_id]).pages.find(params[:id])
@original = User.find(params[:user_id]).pages.find(@page.duplicate_of)
if @original.update_attributes(params[:page])
@page.destroy
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.