Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 486
Ringo Blancke Avatar asked Jun 28 '13 16:06

Ringo Blancke


7 Answers

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!

like image 104
Ringo Blancke Avatar answered Nov 20 '22 19:11

Ringo Blancke


I found adding this to your header helps.

<%= include_gon(:init => true) %>

Works great for rails 4 as well.

like image 37
Adam Bishti Avatar answered Nov 20 '22 21:11

Adam Bishti


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: enter image description here


Read more here

like image 35
ahmet Avatar answered Nov 20 '22 20:11

ahmet


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.

like image 24
user456584 Avatar answered Nov 20 '22 19:11

user456584


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.

like image 1
Gwen Au Avatar answered Nov 20 '22 21:11

Gwen Au


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.

like image 1
Steve Wollkind Avatar answered Nov 20 '22 21:11

Steve Wollkind


You should add

<%= Gon::Base.render_data %>

to the head section of your application.html.erb

like image 1
Guy Dubrovski Avatar answered Nov 20 '22 21:11

Guy Dubrovski