lazy_high_charts
lazy_high_charts copied to clipboard
Undefined Method 'headers' without request object but with Turbolinks included
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
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.
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!