Paper_Trail:显示版本之间的差异 [英] Paper_Trail: Show Diff Between Versions
问题描述
我是Rails的新手...使用RubyMine作为IDE.
I'm new to Rails...using RubyMine as an IDE.
我让Paper_Trail保存了数据"xoi_qb"的先前版本.我的视图当前正在显示当前和以前的数据,但是我想显示当前版本"xoi_qb"和先前版本"xoi_qb"之间的差异.例如,当前版本可能是"97",而先前版本可能是"94",我想显示"XOI +/-:+3".我想显示这种差异,并根据积极或消极的变化添加"+"或-".
I have Paper_Trail saving previous versions of the data "xoi_qb". My view is currently showing the current and previous data as I'd like, but I would like to show the diff between the current version "xoi_qb" and the previous version "xoi_qb". For instance, the current version may be "97" and the previous version may be "94", and I would like to display "XOI +/-: +3". I would like to display this difference and add the "+" or "-" based on the positive or negative change.
在我的模型中,Paper Trail设置为创建如下版本:
In my model, Paper Trail is set to create versions like this:
def get_xoi_qb
xoi_qb = []
self.versions.each do |version|
unless version.reify.nil?
xoi_qb << version.reify.xoi_qb
end
end
return xoi_qb
end
在我的HTML中设置为显示以下版本:
And in my HTML set to display the versions like this:
<th>Previous XOI</th>
<table>
<% @quarterback.versions.each do |version| %>
<tr>
<td><%= version.reify.xoi_qb %> dated <%= version.created_at %></td>
</tr>
<% end %>
不确定如何显示两者之间的差异.
Not sure how to show the difference between the two.
真的很感谢您的帮助.
推荐答案
看看有关PaperTrail的版本差异的文档.特别是,这是您要注意的:
Take a look at the documentation on diffing versions with PaperTrail. In particular, here's what you want to take note of:
如果您在 versions 表中添加 object_changes 文本列,则在安装时使用 rails生成paper_trail:install --with-changes 选项或手动,PaperTrail将在每个更新版本中存储更改差异(不包括PaperTrail忽略的任何属性).
If you add an object_changes text column to your versions table, either at installation time with the rails generate paper_trail:install --with-changes option or manually, PaperTrail will store the changes diff (excluding any attributes PaperTrail is ignoring) in each update version.
启用此行为后,获取 object.versions.map {| v | [v.created_at,v.changeset]} ,然后遍历该结构以呈现您的更改日志.
With this behavior enabled, it is reasonably simple to get object.versions.map{|v| [v.created_at, v.changeset]} and then iterate over that structure to render your change log.
这篇关于Paper_Trail:显示版本之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!