Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
963 views
in Technique[技术] by (71.8m points)

ruby on rails - gon is not defined error in javascript

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

gon.preference = "hello"

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

console.log(gon.preference)

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.

No clue why gon is just not working!

Help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...