gazay / gon

Your Rails variables in your JS
MIT License
3.05k stars 184 forks source link

include_gon helper is not supports on Rails 4 #90

Open krishnasrihari opened 11 years ago

krishnasrihari commented 11 years ago

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

It solves my problem in Rails 4

gazay commented 11 years ago

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 commented 11 years ago

Any progress on this?

gazay commented 11 years ago

Not yet =(

steakchaser commented 11 years ago

@saturday can you give this a try:

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

saturday commented 11 years ago

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 commented 10 years ago

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

worked for me :)

gazay commented 10 years ago

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 commented 10 years ago

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

SteKearns commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

Thanks a lot, I'll dig into it

manmartinez commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

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

sebastialonso commented 10 years ago

@gazay I will, thanks.

kaungst commented 10 years ago

@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 commented 10 years ago

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

schuberm commented 10 years ago

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 commented 9 years ago

init worked for me

randomprimate commented 9 years ago

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 commented 9 years ago

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

h8rry commented 9 years ago

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

marvindanig commented 9 years ago

I'm facing this issue on production mode right now. Gon works as expected in development mode but on production I get a SyntaxError.

Rails is outputting the following inside the head tag:

< script > <![CDATA[window.gon={}...]]> </script>

marvindanig commented 9 years ago

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 commented 9 years ago

What would be the full the code for that?

<script type="text/javascript"> <%= include_gon :watch => true, :init => true, :need_tag => false %> </script>
ChasevanHekken commented 9 years ago

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

christian-froh commented 9 years ago

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

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

noppanit commented 9 years ago

I have the same problem as @christianonrails

gazay commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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

TheDerek commented 9 years ago

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

dukha commented 9 years ago

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

gazay commented 9 years ago

@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 commented 9 years ago

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 https://github.com/gazay/gon/issues/90#issuecomment-87474582.

LinKassem commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 8 years ago

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 commented 8 years ago

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

philipyoo commented 8 years ago

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 commented 8 years ago

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 https://github.com/gazay/gon/issues/90#issuecomment-150853794.

ghost commented 8 years ago

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 https://github.com/gazay/gon/issues/90#issuecomment-150853794.

jibiel commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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