Paper_trail with accepts_nested_attributes_for

你离开我真会死。 提交于 2020-01-06 13:51:55

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/6351572/paper-trail-with-accepts-nested-attributes-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!