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?
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!
I found adding this to your header helps.
<%= include_gon(:init => true) %>
Works great for rails 4 as well.
I created a test project to attempt to see if I have the same problem, Which I DID!
Solution add the tag <%= include_gon %>
in your header within application.html.erb
:
app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<%= include_gon %>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
In the view:
console.log(gon.preference)
In the controller:
gon.preference = "hello"
Output:
Read more here
An alternative to @Ringo's solution would be to leave the statement in the application layout file, simply modifying it as follows:
<%= include_gon if defined? gon && gon.present? %>
This keeps the include_gon
reference as DRY as possible while still being as flexible and unobtrusive.
I had the same error message and also used user456584's answer but the error message remained. Once I added gon.event = @event or gon.events = @events as below in the controller, I guess gon became defined and therefore, I no longer had the error message "gon is not defined".
def show
@event = Event.find(params[:id])
gon.events = @events
respond_to do |format|
format.html # show.html.erb
format.json { render json: @event }
end
end
I am a new user and I was only trying to contribute. This was the only method that worked for me after all other suggestions.
I just ran across what I think is the same issue myself, and I'm wondering if the include_gon call should be in the body rather than the header of the application.html.erb. Putting a js alert in both the header and body of application.html.erb shows that the header is not always invoked, while the body is.
I was experiencing a lack of gon if I initially loaded a page on my site other than the one that used it, and then navigated there via header links, but if I initially loaded the gon-using page, the site would work fine until a reload on any other page.
Moving include_gon from the header to the body of the layout seems to have fixed this. I realize this is not how the gon docs suggest this should be done, but I'm not understanding how it would work the other way.
You should add
<%= Gon::Base.render_data %>
to the head
section of your application.html.erb
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With