Sorry this page looks weird. It was automatically migrated from my old blog, which had a different layout and different CSS.

Undo and Redo with PaperTrail

Yesterday Ryan Bates released an excellent RailsCast showing how to implement undo and redo in your Rails app using PaperTrail.

Instead of asking the user to confirm they want to delete something, for example, you just delete it and add an undo link to the model’s flash message. Similarly for update and create.

I’m all in favour of getting out of the user’s way and doing what they want, so I liked this Railscast a lot.

I must say Ryan makes more efficient use of my gem than I do in my own apps!

Restoring Associations

Consider a model which has many dependent children.

class Book < ActiveRecord::Base
  has_many :chapters, :dependent => :destroy
  has_paper_trail
end

class Chapter < ActiveRecord::Base
  belongs_to :book
  has_paper_trail
end

When you destroy a book, its chapters are destroyed and PaperTrail stores the book’s and chapters' versions. If you undo the book’s deletion, PaperTrail doesn’t yet automatically restore the chapters. I wish it would; I simply haven’t found a good way to implement this yet.

My current thinking is to make PaperTrail aware of requests or transactions, perhaps like Efficiency’s transaction ID middleware, though to date I haven’t found time to try it.

So if you’re looking for a way to help, please solve this!

Andrew Stewart • 1 March 2011 • PaperTrailRails
You can reach me by email or on Twitter.