Kris Kris - 5 months ago 11
Ruby Question

ROR return JSON with 406 Not Acceptable error

When we return JSON output using

render :json =>@profiles
, the
output will return the required results with a 406 error. How can
avoid that '406 Not Acceptable' error ?


I'm more than sure that you have this problem.


Say your controller only returns json answers

def action
  # call
  respond_to do |format|
    format.json { render json: results }

This will return the json as soon as:

  • /path_to_action.json is called
  • /path_to_action is called with headers Content-Type:application/json; and probably some other header types (Eg. X-Requested-With:XMLHttpRequest)

Otherwise, it returns a 406 Not Acceptable error.

To avoid the issue, if your controller only returns json, write:

def action
  # call
  render json: results

otherwise, use /path_to_action.json instead.