I'm using Rails as a JSON-only API server but whenever there is an exception in the a controller, say attempting to delete a record with an ID of 1 when it doesn't exist, Rails doesn't respond with JSON it responds with HTML, either a trace if in development or a generic 'something went wrong' page in production.
Right now I'm wrapping everything in a rescue and spitting out a JSON response manually...
class AmazonAccountsController < ApplicationController
respond_to :json, :xml
def destroy
# Handle bad API calls.
begin
@account = AmazonAccount.find(params[:id])
@account.destroy
# unrelated code...
rescue
render :json => {:errors => {:bad => "ID doesn't exist."}}.to_json
end
end
end
but this doesn't seem like the ideal way to handle this.
This is in Rails 3.