Buffalo v0.11.0中flash与application.html报错求助
解决Buffalo authrecipe中
flash: unknown identifier错误 嘿,我完全懂你作为Golang和Buffalo新手碰到这个错误的挫败感——刚上手就卡在模板变量的问题上,确实挺闹心的。咱们一步步拆解解决:
问题根源
从错误日志能看出来,你的_flash.html模板第3行在调用flash变量,但这个变量根本没被传到模板的渲染上下文里。哪怕你删了相关指令还报错,十有八九是Buffalo的模板缓存没更新导致的。
具体解决步骤
确保Handler把Flash数据传入上下文
Buffalo不会自动把Flash数据注入所有模板,你得在对应的路由处理函数里手动把Flash变量放到上下文里,比如:func YourHandler(c buffalo.Context) error { // 其他业务逻辑 c.Set("flash", c.Flash()) return c.Render(http.StatusOK, r.HTML("your_page.html")) }如果用的是authrecipe自带的Handler,也得检查它的代码有没有做这一步,要是没有就手动补上。
清理模板缓存
Buffalo会缓存编译后的模板来提速,有时候改了模板缓存却没同步更新。你需要:- 先停掉
buffalo dev进程 - 删除项目根目录下的
tmp文件夹 - 重新启动
buffalo dev
这一步能保证加载的是你最新修改后的模板内容。
- 先停掉
检查模板引用的正确性
要是你之前在application.html里引用了_flash.html,得确认引用语法是对的:<%= partial("_flash.html") %>同时要保证
_flash.html确实在templates目录下,路径没写错。
你提供的相关代码
application.html
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta charset="utf-8"> <title>Buffalo - Coke</title> <%= stylesheetTag("application.css") %> <meta name="csrf-param" content="authenticity_token" /> <meta name="csrf-token" content="<%= authenticity_token %>" /> <link rel="icon" href="<%= assetPath("images/favicon.ico") %>"> </head> <body> <div class="container"> <%= yield %> </div> <%= javascriptTag("application.js") %> </body> </html>
_flash.html
<div class="row"> <div class="col-md-12"> <%= for (k, messages) in flash { %> <%= for (msg) in messages { %> <div class="alert alert-<%= k %>" role="alert"> <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button> <%= msg %> </div> <% } %> <% } %> </div> </div>
内容的提问来源于stack exchange,提问作者Shilpa Kancharla
相关产品推荐
相关产品推荐

