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

Undefined Method 'headers' without request object but with Turbolinks included #194

Open
MauricioMoraes opened this issue Jun 8, 2015 · 2 comments

Comments

@MauricioMoraes
Copy link

We've been using LazyHighCharts in our system for a long time now, and now we're refactoring it to use Turbolinks.

Now I'm getting this error:

 ActionView::Template::Error (undefined method `headers' for nil:NilClass):
 ...

It happens when I call the method high_chart on my view.

The problem is that, due to the way I've structured my code, I don't have the request object, so it crashes right here:

lazy_high_charts/lib/lazy_high_charts/layout_helper.rb lines 37 to 69, most specifically in line 45

 if defined?(request) && request.respond_to?(:xhr?) && request.xhr?
     ...
 elsif defined?(Turbolinks) && request.headers["X-XHR-Referer"]  # here!
     ... 
 else
     ...
 end

I could refactor all my code and call the controller without the send_data method, as I'm doing, just to have the request object, but it makes sense to me to verify if the request object exists on line 45, as its done on line 37 of the file above. This will make the code more robust, and doesn't seem to break anyone's code.

Then, line 45 would become:

  elsif defined?(Turbolinks) && defined?(request) && request.headers["X-XHR-Referer"]

Does it make sense to you? Should I make a pull request?

Thanks

@MauricioMoraes MauricioMoraes changed the title Undefined Method 'headers' without requests but with Turbolinks included Undefined Method 'headers' without request object but with Turbolinks included Jun 8, 2015
MauricioMoraes added a commit to MauricioMoraes/lazy_high_charts that referenced this issue Jun 9, 2015
This commit solves issue michelson#194.
The request object is verified on the if clause but not on the elsif part, breaking when calling the headers method on the object that doesn't exist.
This is an inprovement on the code robustness that doesn't seem to break anyone's code.
MauricioMoraes added a commit to MauricioMoraes/lazy_high_charts that referenced this issue Jun 9, 2015
Still on issue michelson#194.
The request method could be defined and still be returning nil.
With respond_to? on line 37, it already covered the case where request was undefined. It's crearer without the "defined?" method.
@KCErb
Copy link
Contributor

KCErb commented Sep 15, 2015

Hi there, ran into a similar issue today except for me request is defined but doesn't respond to headers. Seems that a decent solution is to change line 45 (As stated above) to match the first test like so:

defined?(Turbolinks) && defined?(request) && request.respond_to?(:headers) && request.headers["X-XHR-Referer"]

I'd be happy to submit a PR if you're interested.

@MauricioMoraes
Copy link
Author

Hi @KCErb! That's great it's not just happening to me! I'm already using this branch in production, and that's why I forked it.
Go ahead! PR it!

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