Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include_gon helper is not supports on Rails 4 #90

Open
krishnasrihari opened this issue Jul 5, 2013 · 51 comments
Open

include_gon helper is not supports on Rails 4 #90

krishnasrihari opened this issue Jul 5, 2013 · 51 comments

Comments

@krishnasrihari
Copy link

No description provided.

@krishnasrihari
Copy link
Author

use this helper to use in Rails <%= include_gon(:init => true) %>

It solves my problem in Rails 4

@gazay
Copy link
Owner

gazay commented Jul 9, 2013

Thank you for issue! Unfortunately I haven't touch rails 4 because of scope of my current work. I'll try to dig into by myself when I get some time but it will be super if can help me to realize where is the problem or maybe make some falling test) Thanks again!

@saturday
Copy link

Any progress on this?

@gazay
Copy link
Owner

gazay commented Sep 12, 2013

Not yet =(

@steakchaser
Copy link
Contributor

@saturday can you give this a try:

<%= Gon::Base.render_data({}) %>

@saturday
Copy link

Both of these create the gon object:

<%= include_gon(:init => true) %>
<%= Gon::Base.render_data({}) %>

However they end up being empty js objects.

If I don't use either of the above the gon object isn't created at all as @krishnasrihari mentioned.

@touchdown
Copy link

<%= Gon::Base.render_data({}) %>

worked for me :)

@gazay
Copy link
Owner

gazay commented Sep 23, 2013

I can't reproduce this on new rails 4 application. Helper is working for me. Can someone provide simple app which reproduce this issue please?

@SteKearns
Copy link

The helper was working for me in my 4.0.0 project but now it isn't... trying to figure out what changed

@SteKearns
Copy link

OK... embarrassingly (being new to Ruby on Rails), I have declared my index action after the protected keyword but because my view was still loading, I didn't notice.

The gon view helper is working just fine for me in Rails 4

@gazay
Copy link
Owner

gazay commented Oct 11, 2013

So I still can't reproduce not working helper in rails 4. It was stopper for release new version, and I think without sample application with not working gon helper I'll release new version in few days without some fixes for this issue.

@manmartinez
Copy link

I think I'm experiencing this very same issue, sometimes I get a gon is not defined error even though the <%= include_gon %> helper is in my layout. I've noticed that the issue goes away whenever I change some partial rendering in my app, namely when I change <%= render 'form' %> to <%= render partial: 'form' %> Funny thing is the error only goes away until I shut down the server, whenever I start the server again I have to change that line again, only now I go from <%= render partial: 'form' %> to <%= render 'form' %> (which I thought was the problem) and it fixes things again until I restart the server

You can see my app here

Hope it helps!

@gazay
Copy link
Owner

gazay commented Nov 13, 2013

Thanks a lot, I'll dig into it

@manmartinez
Copy link

Hey, so I kept working on that app and realize the <%= render partial: 'form' %> had nothing to do with it, it actually came down to just reloading the page. And so I realized that in my case it had to do with turbolinks, I didn't know that the document.ready event doesn't work with turbolinks and that I had to take into account the events fired by turbolinks. Since I didn't had much time I just removed turbolinks from my app and that solved my issue.

@sebastialonso
Copy link

what @touchdown said work for me!
I'm on Rails 4, Ruby 2, and without TurboLinks

I had to pull directly from the repo, though
gem 'gon', github: 'gazay/gon'

EDIT: after finally getting the helper to work, gon is just doing random things, sometimes saves, sometimes not. Has someone experienced that?

@gazay
Copy link
Owner

gazay commented Feb 14, 2014

@sebastialonso try new 5.0.4 version, it should work without workarounds.

@sebastialonso
Copy link

@gazay I will, thanks.

@gazay gazay closed this as completed Feb 16, 2014
@kaungst
Copy link

kaungst commented Feb 26, 2014

@gazay Hey, just wanted to echo that I'm having similar issues with gon, namely that it was undefined. Found that calling "window.gon.variable_name" in my javascript was a successful workaround.

Side note, really great gem, extremely useful, thanks for your help/work!

@gazay
Copy link
Owner

gazay commented Mar 3, 2014

@vorDd can you tell what exactly was the problem? I'll try to research it

@gazay gazay reopened this Mar 3, 2014
@schuberm
Copy link

Not sure what the current status of gon is. Using rails 4.1.1 and ruby 2.1 and gon 5.1.2, I'm getting ReferenceError: gon is not defined. I've tried the suggestions in this thread without success:

<%= include_gon(:init => true) %> throws no implicit conversion of nil into Hash
<%= Gon::Base.render_data({}) %> throws no implicit conversion of nil into Hash
window.gon.variable_name throws TypeError: window.gon is undefined

@richpeck
Copy link

init worked for me

@randomprimate
Copy link

Rails 4.1.7, Ruby 2.1.2 and Gon 5.2.3. Same issue getting ReferenceError: Can't find variable: gon. Any help appreciated.

@schuberm
Copy link

I never sorted out my problem with gon, realized that websockets was closer to what I needed.

@h8rry
Copy link

h8rry commented Nov 30, 2014

include_gon(:init => true) didn't completely work for me... What exactly is the problem?

@marvindanig
Copy link

Ok, here's how we fixed it. The problem was that Gon wasn't spitting out properly encoded script tags, so I set :need_tag => false and pasted the following code into the head:

/ Pass Rails Variable to JS
%script{ :type => 'text/javascript' }
= include_gon :watch => true, :init => true, :need_tag => false

Works as expected now.

@ChasevanHekken
Copy link

What would be the full the code for that?

<script type="text/javascript"> <%= include_gon :watch => true, :init => true, :need_tag => false %> </script>

@ChasevanHekken
Copy link

My gon code is working on local host but not on a digital ocean VPS, any suggestions?

mohsinali added a commit to mohsinali/gon that referenced this issue Feb 6, 2015
The reason for this change is that in rails4 the "<%= include_gon %>" throws 'gon not found' error. 
I was facing this issue with my rails 4.1 app. And it wasted my time a lot. I would suggest that for now, if we can add the solution given in gazay#90 in the readme file will help to others a lot.
This was referenced Feb 6, 2015
@christian-froh
Copy link

I just created a rails 4.2.0 app and gon isnt working :(

= Gon::Base.render_data({})
error => not opened for reading

@noppanit
Copy link

I have the same problem as @christianonrails

@gazay
Copy link
Owner

gazay commented Mar 12, 2015

Hello all! Please provide smallest app which reproduces this error – when I'm creating new rails 4.0-4.2 apps I don't have any problem with include_gon helper. But maybe I'm using wrong web server (for reproducing needed puma for example or something else). Please help me to trace this error, I'm in process of refactoring and test covering gon for new version and it would be great if in new version it would be fixed.

@bobbdelsol
Copy link

I see the same issue whenever I go to the page from a page without gon, specifically from the Rails_admin page (different engine.) Using windows.gon.variable solved it. So the turbolinks gem for waiting on page load must not work in that case.

@Eilie
Copy link

Eilie commented Mar 17, 2015

Neither = Gon::Base.render_data({}) nor include_gon is working with rails 4.2 on thin.
uninitialized constant ActionView::CompiledTemplates::Gon

@TheDerek
Copy link

Gon is not working on the latest version of rails with or without turbolinks. Please fix.

@dukha
Copy link

dukha commented Mar 28, 2015

Using Rails 4.2 and Puma. Tried include_gon and include_gon(:init=> true_ and Base.render_data. The only time gon worked was the first time after installing the gem

@gazay
Copy link
Owner

gazay commented Mar 29, 2015

@TheDerek can you provide example app that reproduces this error please? In my new projects on rails 4.0-4.2 I can't reproduce it.

@TheDerek
Copy link

It seemed to have fixed itself when I restarted the rails server. I've
decided to use Ajax now instead as it makes my life a lot easier and means
I actually need to build an API now which I've been putting off for far to
long.

On Sun, Mar 29, 2015 at 9:55 PM, Alexey Gaziev notifications@github.com
wrote:

@TheDerek https://github.com/TheDerek can you provide example app that
reproduces this error please? In my new projects on rails 4.0-4.2 I can't
reproduce it.


Reply to this email directly or view it on GitHub
#90 (comment).

@LinKassem
Copy link

I am working on a Rails 4 application and I used to have the same error, but adding
<%= include_gon(:init => true) %>
<%= Gon::Base.render_data({}) %>
in the application.html.erb solved the issue.

@tyronewilson
Copy link

I used = include_gon(init: true) and then did and (full OS) server restart and it then worked. I think it could be an environment loading issue.

@rept
Copy link

rept commented Jun 23, 2015

Just to confirm. Prior to installing this gem I searched the issues to make sure it would work with turbolinks and rails >=4. I can confirm it works if you use

<%= include_gon(:init => true) %>
<%= Gon::Base.render_data({}) %>

Maybe best to include this in the readme because now it says:

<%= Gon::Base.render_data %>

and that doesn't work.

BTW: Thanks for the gem!

@jmarceli
Copy link

As for gon 5.2.3 it was enough for me to write:

<%= include_gon(init: true) %>

OR:

<%= Gon::Base.render_data({}) %>

There was no need for specifying both because they are doing same thing.

BTW: README update would be nice :)

@dongli
Copy link

dongli commented Aug 1, 2015

I also encountered this problem. I have used all the possible lines in the application.html.erb, but the variable is always undefined. Any idea? The version of gon is 6.0.1.
In research_records_controller.rb:

def update
    respond_to do |format|
      if @research_record.update(research_record_params)
        if @research_record.tag_draft
          gon.content = ApplicationHelper.markdown @research_record.content
          format.js { render action: :preview }
...

In preview.js.erb:

alert(gon.content);

gon

@ghost
Copy link

ghost commented Sep 24, 2015

I am having the same issue with Rails 4.2.3 ruby 2.1.2.

I also confirmed this worked and it would be nice if it were on the GitHub landing page info.
<%= Gon::Base.render_data({}) %>

Still in the javascript the compiler does not like the gon. prefix as a variable name. Basically it does not work any help would be nice. I am going to try to dig deeper but someone with code experience could find it much faster. Help Thanks for the gem if it worked it would be of great use.

@iroller
Copy link

iroller commented Oct 18, 2015

Rails 4.2.0, include_gon works just fine here without "Gon::Base.render_data"

@philipyoo
Copy link

Using Rails 4.2.4 w/ Turbolinks, was getting undefined after creating a new object and going back to the index page. My fix was to move <%= include_gon(init: true) %> from the <head> to the <body> and it seems to be working fine now. I don't see any immediate bugs after this change

@ghost
Copy link

ghost commented Oct 24, 2015

Thank You I will give this a try.

On Sat, Oct 24, 2015 at 2:50 PM, Philip Yoo notifications@github.com
wrote:

Using Rails 4.2.4 w/ Turbolinks, was getting undefined after creating a
new object and going back to the index page. My fix was to move <%=
include_gon(init: true) %> from the to the and it seems to
be working fine now. I don't see any immediate bugs after this change


Reply to this email directly or view it on GitHub
#90 (comment).

@ghost
Copy link

ghost commented Oct 25, 2015

FYI -
I am running Rails 4.1.4, I have not upgraded to the latest version, we are
in a code freeze.

On Sat, Oct 24, 2015 at 4:24 PM, Wayne Mattingly whoru7777777@gmail.com
wrote:

Thank You I will give this a try.

On Sat, Oct 24, 2015 at 2:50 PM, Philip Yoo notifications@github.com
wrote:

Using Rails 4.2.4 w/ Turbolinks, was getting undefined after creating a
new object and going back to the index page. My fix was to move <%=
include_gon(init: true) %> from the to the and it seems to
be working fine now. I don't see any immediate bugs after this change


Reply to this email directly or view it on GitHub
#90 (comment).

@jibiel
Copy link

jibiel commented Nov 19, 2015

I've started using gon (with include_gon helper) in our fresh rails 4.2.4 app with turbolinks a couple of months ago. Never had any problems with it, works as advertised. I guess y'all should try to deduce it to some legacy code or incompatible dependencies.

@ptyagi16
Copy link

FWIW I can reproduce this problem if you use render stream: true in your controller action. If you don't use render stream: true it works fine with `<%= include_gon %>

@gouravtiwari
Copy link

Gon fails on render stream: true for me too on Rails 4.2.6, any work around for this one?

@rajgiri8
Copy link

rajgiri8 commented Dec 5, 2019

I am having the same issue, it's returning empty {} when called from application.js but when print it in the controller, the variable gets assigned. I have this tag in application.html.haml '= include_gon :watch => true, :init => true', and simple assignment in controller gon.your_int = 5. I was working on my school project and was trying to see if someone can help me with this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests