Rails应用回滚提交后出现ActionView语法错误,求排查与CoffeeScript修正
Rails回滚后CoffeeScript语法错误的修复
错误日志
Processing by MainController#index as HTML Rendering layout layouts/lmt.html.erb Rendering main/index.html.erb within layouts/lmt Event Load (1.0ms) SELECT "events".* FROM "events" ↳ app/views/main/index.html.erb:14 Rendered main/index.html.erb within layouts/lmt (Duration: 357.4ms | Allocations: 238507) Rendered layout layouts/lmt.html.erb (Duration: 357.6ms | Allocations: 238584) Completed 500 Internal Server Error in 358ms (ActiveRecord: 1.0ms | Allocations: 239010) ActionView::Template::Error (SyntaxError: [stdin]:15:1: unexpected indentation): (execjs):7539:8 (execjs):7557:14 (execjs):1:32
问题根源
报错指向execjs但并非该文件本身有问题,而是你的application.coffee存在缩进错误——CoffeeScript依赖严格的缩进划分代码块,错误的缩进会导致编译失败。
错误的CoffeeScript代码
# app/assets/javascript/packs/application.coffee setup = -> $(document).foundation(); $ -> console.log "Loading page...! " + Date.now()%10000 setup() document.addEventListener "turbolinks:load", () -> console.log "turbolinks:load " + Date.now()%10000 setup()
修复后的代码
# app/assets/javascript/packs/application.coffee setup = -> $(document).foundation() $ -> console.log "Loading page...! " + Date.now()%10000 setup() document.addEventListener "turbolinks:load", -> console.log "turbolinks:load " + Date.now()%10000 setup()
修复说明
- 统一缩进层级:每个函数(
->)后的代码必须缩进一层,确保代码块归属正确,这是CoffeeScript的核心语法规则 - 移除冗余分号:CoffeeScript会自动处理语句结尾,无需手动添加分号
- 修正回调代码块:
turbolinks:load的回调函数内部,console.log和setup()都需缩进至回调函数层级,避免出现意外缩进的语法错误
内容的提问来源于stack exchange,提问作者John Cowan
相关产品推荐
相关产品推荐

