Rails request.xhr? returns 0

I am trying to determine if the request is xhr or not in ApplicationController using

request.xhr? 

It always returns 0.

But the headlines say otherwise;

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

What am I missing?

version - Rails 4.0.0

+6
source share
1 answer

request.xhr? always returns Object or nil , not true or false , since it is based on matching with a regular expression pattern (see here ).

In Ruby, any value other than false and nil is the true value (like the 0 returned by request.xhr? ), So the answer is correct.

+9
source

Source: https://habr.com/ru/post/974502/


All Articles