观察Rails ActionText更改的最简单方法是什么?我的用例是在编辑完富文本字段后发送通知。
因为ActionText创建了一个到模型的关联,而不是模型上的属性,所以Dirty模块不可用。
我可以使用自定义脏关联模块,例如:https://anti-pattern.com/dirty-associations-with-activerecord
我还可以在模型上有一个属性,它在每次更新时保存ActionText::内容的纯文本,然后观察。
还有没有别的选择?
发布于 2021-01-29 22:33:28
您可以使用如下所示的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
...
endhttps://stackoverflow.com/questions/65948472
复制相似问题