开发者

Paper_trail with accepts_nested_attributes_for

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 resto开发者_如何学JAVAred/updated when a user clicks undo?


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜