rails: request.xhr? is not working in controller

class AlphaController < ApplicationController
  def home
    @microblogs = Microblog.all

    binding.pry

    if request.xhr?
        render :json => {
            :microblogs => @microblogs
        }
    end
  end
end

I wrote this action and used request.xhr? method to tell if the request is sent via ajax. But what I found in debugging is that the result returned by xhr? is always nil. A ruby method whose name ends with question mark is supposed to always return true or false, isn’t it?

2014-08-10 1 16 29

I wonder if request.xhr? is deprecated in Rails 4.1.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

Another approach

def xhr_or_js?
  request.xhr? || request.format.js?
end