Paper_trail 与 Accepts_nested_attributes_for

发布于 2024-11-15 08:34:10 字数 243 浏览 5 评论 0原文

我有一个 Rails 应用程序,其中包含文章,用户可以将链接和评论添加为嵌套属性。

我在 paper_trail https://github.com/airblade/paper_trail/ 文档中看到这不是被那颗宝石覆盖。我将如何设置撤消功能,以便当用户单击撤消时恢复/更新嵌套属性或 has_many 关联?

I have a rails app that has articles that and the user can add links and reviews as nested attributes.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

握住你手 2024-11-22 08:34:10

我认为,如果您将“销毁”帖子挂接到撤消按钮,则在单击撤消时至少会删除链接。基本上,您通过特殊的 _destroy 键传递一个哈希,它将删除嵌套的模型记录。

从 Rails 3 文档此处

class Member < ActiveRecord::Base
   has_one :avatar
   accepts_nested_attributes_for :avatar, :allow_destroy => true
end

现在,当您将 _destroy 键添加到属性哈希,其值为 true,您将销毁关联的模型:

member.avatar_attributes = { :id => '2', :_destroy => '1' }
member.avatar.marked_for_destruction? # => true
member.save
member.reload.avatar # => nil

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.

From Rails 3 docs here:

class Member < ActiveRecord::Base
   has_one :avatar
   accepts_nested_attributes_for :avatar, :allow_destroy => true
end

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文