带有 accepts

编程入门 行业动态 更新时间:2024-10-26 10:33:07
本文介绍了带有 accepts_nested_attributes_for 的 Paper_trail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

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

I saw in the paper_trail github/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

本文发布于:2023-10-18 03:12:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1502906.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:accepts

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!