Something that confused the hell out of me, was when I wanted to so some AJAX request, I wanted to use methode .xhr?
in the controller, to see, whether it was an ajax or regular request.
Turns out a request by a form or a link with remote: true
, does not contain the header X-Requested-With
that this method checks for.
So why include a handy method to the request object, if it is not usable with a standard ajax request.
for reference from ActionDispatch::Request
# Returns true if the "X-Requested-With" header contains "XMLHttpRequest"
# (case-insensitive), which may need to be manually added depending on the
# choice of JavaScript libraries and frameworks.
def xml_http_request?
get_header("HTTP_X_REQUESTED_WITH") =~ /XMLHttpRequest/i
end
alias :xhr? :xml_http_request?