带有 accepts_nested_attributes_for 的 Paper_trail [英] Paper_trail with accepts_nested_attributes_for
问题描述
我有一个包含文章的 rails 应用程序,用户可以将链接和评论添加为嵌套属性.
I have a rails app that has articles that and the user can add links and reviews as nested attributes.
我在 paper_trail https://github.com/airblade/paper_trail/ 文档中看到这不是那颗宝石所涵盖的.我将如何设置撤消功能,以便在用户单击撤消时恢复/更新嵌套属性或 has_many 关联?
I saw in the paper_trail https://github.com/airblade/paper_trail/ documentation that this is not covered by that gem. How would I go about setting up undo functionality so that nested attributes or has_many associations are restored/updated when a user clicks undo?
推荐答案
我认为,如果您将销毁"帖子连接到撤消按钮,它至少会删除链接(如果它们单击撤消).基本上,您使用特殊的 _destroy
键传递一个哈希值,它将删除嵌套的模型记录.
I think if you hook in a "destroy" post to the undo button it will at least remove the links if they click undo. Basically you pass a hash with the special _destroy
key it will remove the nested model records.
来自 Rails 3 文档此处:
From Rails 3 docs here:
class Member < ActiveRecord::Base
has_one :avatar
accepts_nested_attributes_for :avatar, :allow_destroy => true
end
现在,当您将 _destroy 键添加到属性哈希值时,如果值为 true,您将销毁关联的模型:
Now, when you add the _destroy key to the attributes hash, with a value that evaluates to true, you will destroy the associated model:
member.avatar_attributes = { :id => '2', :_destroy => '1' }
member.avatar.marked_for_destruction? # => true
member.save
member.reload.avatar # => nil
这篇关于带有 accepts_nested_attributes_for 的 Paper_trail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!