ruby-on-rails - 观察 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
推荐阅读
- neural-network - 反向传播中的梯度维度
- reactjs - 自动导入总是提示文件类型不正确(tsx -> js),如何解决?
- windows - 如何修改以前的代码以检测部分覆盖的表单
- c# - dll构建成功,但加载dll时执行错误
- python-3.x - 如何将 Essentia 音频包从 Home-brew 链接到 Anaconda venv 以使用 Juypter 调用 Essentia 包
- python - SQLAlchemy:组内不同
- python - lgb.train ValueError:具有多个元素的数组的真值不明确。使用 a.any() 或 a.all()
- python - 从函数内创建python线程导致错误
- spring - spring mvc i18n:设置默认语言环境似乎回退到“en”
- javascript - 使用 Angular 8 的上一个、下一个和返回功能,打字稿