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

Categories

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

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

1 Answer

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...