如何解决观察 Rails ActionText 的变化
观察 Rails ActionText 变化的最简单方法是什么?我的用例是在编辑富文本字段时发送通知。
因为 ActionText 创建与模型的关联并且不是模型上的属性,所以 Dirty 模块不可用。
我可以使用自定义的脏关联模块,例如:https://anti-pattern.com/dirty-associations-with-activerecord
我也可以在模型上有一个属性,它在每次更新时保存 ActionText::Content 纯文本,然后被观察。
还有其他选择吗?
解决方法
您可以像下面这样使用 hash
class ArticlesController < ApplicationController
...
def update
@article = Article.find(params[:id])
orig_content_hash = @article.content.to_s.hash
if @article.update(article_params)
# hash will have changed if content was updated
send_notification unless @article.content.to_s.hash == orig_content_hash
redirect_to article_path(@article.id)
else
render :edit
end
end
...
end
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。