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
相关产品推荐
相关产品推荐

