gpt4 book ai didi

ruby-on-rails - Rails - 检查记录是否已更新

转载 作者:数据小太阳 更新时间:2023-10-29 08:43:32 25 4
gpt4 key购买 nike

我正在尝试检查一条记录是否已被更新/编辑,如果有,那么它应该显示已被编辑。

这是我的 if 语句:

<% if comment.changed? %>
<p id="comment-name"><%= comment.user.name %>(edited)</p>
<% else %>
<p id="comment-name"><%= comment.user.name %></p>
<% end %>

虽然它不起作用。更新评论时,不会显示任何 (edited) 文本。判断评论记录是否更新,需要在if语句中放什么?

最佳答案

我不认为 #changed? 正在做您认为正在做的事情。

#changed? returns true if any attribute have unsaved changes, false otherwise.

Reference


如果记录的 updated_at 不同于 created_at(updated_at 应该大于 created_at 以更准确)。

<% if comment.updated_at != comment.created_at %>
<p id="comment-name"><%= comment.user.name %>(edited)</p>
<% else %>
<p id="comment-name"><%= comment.user.name %></p>
<% end %>

关于ruby-on-rails - Rails - 检查记录是否已更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40731762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com