I am using the acts_as_a开发者_StackOverflow社区udited gem with my application. (Excellent gem to keep track of changes of model objects)
I have been asked to support associating a text comment with each Audit record (similar functionality to svn commit). And I am stumped on how to accomplish this.
For example. Lets say I have an address form, and the user updates City and State, upon hitting save I ask him to provide a Comment. I would like that comment information associated with the audit record that is created.
Make sense?
Thanks for your help,
Jonathan
I thought this would be a useful thing. So I forked and patched the plugin myself.
Edit My fork has been merged into the official acts_as_audited repository. The usage documentation contained in this post is now applicable to vanilla acts_as_audited. I've changed the links in this post to point there. Get the official repository because mine won't be updated with upstream patches. I have updated the usage documentation in this post to reflect enhancements.
Install it as a plugin:
rails_root$ script/plugin install git://github.com/collectiveidea/acts_as_audited.git
Usage doesn't really change from vanilla acts_as_audited.
acts_as_audited takes an extra option now. :require_comment, which if true, blocks creation, updating, or destruction of an audited model unless a comment is supplied.
To add a comment to an audit use model.audit_comment= "My Comment"
before create/update/destroy.
audit_comment can also be mass assigned making it simple to add a comment field to any form.
Before you can use my gem/plugin you'll need to update the audit table to contain a comment column. If your're upgrading from an older version of acts_as_audited that doesn't have a comments field on the audit table, update the plugin then run script/generate audited_migration_update update_audits_table
. Otherwise you're set to go.
With the gem/plugin all that would need to change using your address example adding an audit_comment field to your form.
<%form_for @address do |f| %>
... standard address fields
<%= f.label_for :audit_comment %>
<%= f.text_field :audit_comment %>
<% end %>
精彩评论