简体   繁体   English

Gon无法使用Sinatra中的.js文件

[英]Gon doesn't work with .js files in Sinatra

In my head within layout.erb I included <%= include_gon %> layout.erb head ,我包含了<%= include_gon %>

In main.rb I have written main.rb我写了

require 'gon-sinatra'
Sinatra::register Gon::Sinatra

gon.test = "Test"
puts gon.test

The console outputs Test as expected. 控制台将按预期输出Test

However when I create a .js file and write something like console.log(gon.test) and I look in Firebug or Chrome Console, it says Reference Error: gon is not defined . 但是,当我创建一个.js文件并编写类似console.log(gon.test)并且在Firebug或Chrome控制台中查看时,它显示Reference Error: gon is not defined Why is this happening? 为什么会这样呢? What should I do to fix this? 我应该怎么做才能解决这个问题?

It turns out I have written <script><%= include_gon %></script> which makes it fail. 事实证明,我写了<script><%= include_gon %></script>使其失败。 <%= include_gon %> shouldn't be written inside <script> tags. <%= include_gon %>不应写在<script>标记内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM