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 causes nil object error #48

Closed
dmonopoly opened this issue Jun 1, 2012 · 11 comments
Closed

include_gon causes nil object error #48

dmonopoly opened this issue Jun 1, 2012 · 11 comments

Comments

@dmonopoly
Copy link

In my Rails 3.0.9 app, I recently added gon (which is amazing), but I found that I get this error on a page, "http://localhost:3000/?login=admin1@example.com&password=admin":

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]=
Extracted source (around line #10):

7: <html>
8: <head>
9:  <title>MyApp</title>
10:     <%= include_gon %>
11:     <%= stylesheet_link_tag :all %>
12:     
13:     <!--Include JS lib files-->
(etc.)

I initially go to that url so as to login immediately, which this app requires (the app's actually meant to run in Qt webkit).

The full trace looks like this:

gon (3.0.3) lib/gon/request.rb:27:in `clear'
gon (3.0.3) lib/gon.rb:42:in `clear'
gon (3.0.3) lib/gon/helpers.rb:14:in `include_gon'
app/views/layouts/application.html.erb:10:in `_app_views_layouts_application_html_erb___882655896_2176643980_0'
actionpack (3.0.9) lib/action_view/template.rb:135:in `send'
actionpack (3.0.9) lib/action_view/template.rb:135:in `render'

Interestingly, on other pages, gon seems to work fine...

@gazay
Copy link
Owner

gazay commented Jun 1, 2012

Hey! Thanks for response!

Did you try this on new application or just on your app? What logic in your root action?

@dmonopoly
Copy link
Author

In my app, I actually never go to the root action / "home" page, since the app is meant to run in Qt webkit, and the Qt application makes URL requests to the Rails app like "http://localhost:3000/?login=admin1@example.com&password=admin" and then the Rails app would typically respond with the proper webpage.

I didn't try on a new application, just on this app.

The error occurs in the webpage when I test the url and in the Qt app. (If it doesn't work in the webpage, then it definitely won't work in the Qt app - I just want to point out the error doesn't have anything to do with Qt)

@gazay
Copy link
Owner

gazay commented Jun 1, 2012

I'm sorry but I can't reproduce this bug on new rails app, and don't know your environment. If you can do some debug or tests - it will be helpful! And of course it will be super if you can do pull request with tests for this case or fix for this bug :)

@dmonopoly
Copy link
Author

Ah, I found out the issue: I was declaring Gon.global variables in ApplicationController where there was a before_filter:

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :authenticate!

  # These are constant variables for clear comparison/code readability across controllers and coffee files.
  # So, when a controller action uses Gon to set a variable, the coffeescript can get the same value reliably.
  # These are enums, essentially
  # these should be what controller sets gon.view to
  Gon.global.OTHER_VIEW = 'other'
  Gon.global.PROJECTS_SHOW = 'projects/show'
  Gon.global.FOLDERS_SHOW = 'folders/show'

What would be the proper place to define these global gon variables?

@gazay
Copy link
Owner

gazay commented Jun 1, 2012

I advice to create some initializer for gon and create global variables there if it's possible (if there no dynamic variables). If not - you should do some singleton method which should check define of global variables and place it before call.

@dmonopoly
Copy link
Author

Okay, since the variables are static, I should simply put them in config/initializers/mygonfile.rb ? Will it be automatically included?
Thanks!

@gazay
Copy link
Owner

gazay commented Jun 1, 2012

Yes, it should) If all will be ok - please close the issue)

@dmonopoly
Copy link
Author

Actually the error has to do with setting Gon.global.myvariable anywhere, it seems... when I have it set in an initializer, the error persists. I'm digging around in your code right now, and it all starts here:

gon / lib / gon / helpers.rb

  def include_gon(options = {})
    if variables_for_request_present?
      Gon::Base.render_data(options)
    elsif Gon.global.all_variables.present?
      Gon.clear
      Gon::Base.render_data(options)
    elsif options[:init].present?
      Gon.clear if Gon.all_variables.present?
      Gon::Base.render_data(options)
    else
      ""
    end
  end

The problem might stem from a misunderstanding of your include_gon method... mine has no arguments, yet Gon.clear is still being called (I suspect b/c I set global variables?)

@dmonopoly
Copy link
Author

Could you clarify your usage section? https://github.com/gazay/gon#usage
I don't understand namespace or init or camel case...

@gazay
Copy link
Owner

gazay commented Jun 1, 2012

I found bug I think, I'll do new version tomorrow

Thanks!

About usage sections - I thought there are all simple - you pass some option to include gon and it's change you end javascript

@gazay
Copy link
Owner

gazay commented Jun 2, 2012

fixed in 3.0.4

@gazay gazay closed this as completed Jun 2, 2012
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

2 participants