Kris Kris - 4 months ago 7
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 ?

Answer

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

Explanations:

Say your controller only returns json answers

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

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
end

otherwise, use /path_to_action.json instead.

Comments