You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails片段缓存日志报错:Couldn't find template for digesting: news/new

Rails 6片段缓存日志报错「Couldn't find template for digesting」解决方案

问题场景

在Rails 6.1.7.8版本中使用Memcached实现片段缓存,即便用最简单的缓存代码:

- cache ['v1'] do
  %p Hello World

缓存写入阶段服务器日志仍会重复出现报错:

Couldn't find template for digesting: news/new
Couldn't find template for digesting: news/new
Couldn't find template for digesting: news/new
Read fragment views/home/dashboard/_dashboard_right_components:6e9ed1a420727cad3e0098d33907b18c/v2 (16.5ms)
Write fragment views/home/dashboard/_dashboard_right_components:6e9ed1a420727cad3e0098d33907b18c/v2 (0.9ms)

该报错仅在缓存写入时出现,用户UI无异常,缓存读写功能正常。

排查与解决步骤

  • 修正模板引用路径
    报错指向的news/new模板不存在或路径错误,需全局搜索代码中render 'news/new'或render template: 'news/new'的引用位置:

    • 若应为局部模板,确认是否把_new写成了new(Rails局部模板需以下划线开头命名);
    • 若为动态渲染,检查变量拼接的模板路径是否生成了正确的存在路径。
  • 临时关闭模板摘要生成
    若暂未定位到引用问题,可临时关闭模板缓存的摘要校验,避免报错输出。在对应环境配置文件(如config/environments/production.rb)中添加:

    config.action_view.cache_template_loading = false
    

    注:此为临时排查方案,长期使用会关闭模板缓存的摘要校验,不建议生产环境一直开启。

  • 清理缓存并预编译模板

    • 执行rails cache:clear清理Memcached中的缓存数据;
    • 生产环境执行rails assets:precompile,确保所有模板文件被正确识别并预编译。
  • 规范模板文件名
    确认news目录下的模板命名符合Rails规范:局部模板需以下划线开头(如_new.html.haml),避免将独立模板当作局部模板引用导致找不到摘要。

内容的提问来源于stack exchange,提问作者abhishek77in

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 14:56:02