Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails request.xhr? returns 0

I'm trying to detect whether request is xhr or not in ApplicationController by using

request.xhr?

It always returns 0.

But headers tell otherwise;

request.headers["X-Requested-With"]  ==> "XMLHttpRequest"
@_env['HTTP_X_REQUESTED_WITH'] ==> "XMLHttpRequest"

What am I missing?

version is Rails 4.0.0

like image 458
beydogan Avatar asked Aug 28 '14 17:08

beydogan


1 Answers

request.xhr? always returns Object or nil, not true or false, since it's based on regexp pattern matching (see here).

In Ruby, any value other than false and nil is a truthy value (as is the 0 returned by request.xhr?), so the response is correct.

like image 124
Andrea Salicetti Avatar answered Dec 14 '22 18:12

Andrea Salicetti