本文介绍了gon 未在 javascript 中定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很简单的问题.我正在尝试使用 gon.我在控制器中设置变量是这样的:

pretty simple question. I'm trying to use gon. I set the variable in the controller like this:

gon.preference = "hello"

在我的 .js.erb 文件中,我尝试使用这样的变量:

and in my .js.erb file, I try to use the variable like this:

console.log(gon.preference)

但我收到一条错误消息,指出gon 未定义".可能是什么问题呢?很明显,rails 认识到有一个 gon 变量..js.erb 文件在我的 assets/javascripts 目录中.我尝试将文件名更改为 .js(尽管我根本没想到这会有所作为).显然没有变化.

but I get an error saying that "gon is not defined". What could be the problem? Obviously rails recognizes that there is a gon variable. the .js.erb file is in my assets/javascripts directory. I tried changing the file name to .js (though I didn't expect this to make a difference at all). obviously no change.

不知道为什么 gon 不起作用!

No clue why gon is just not working!

帮助?

推荐答案

这里的答案很好,但弄清楚到底发生了什么.我多次渲染没有布局的特定视图.但是,由于我在重新渲染此视图时没有渲染布局,application.html.erb 中的 <%= include_gon %> 行没有用.

Good answers here but figured out what was going on exactly. I was rendering a particular view without layout multiple times. However, since I was not rendering the layout while re-rendering this view, the <%= include_gon %> line in application.html.erb was useless.

我将 <%= include_gon %> 直接放入我正在渲染的视图中,现在一切正常!

I put <%= include_gon %> directly into the view I'm rendering, and everything works fine now!

这篇关于gon 未在 javascript 中定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 04:43