6

私はレールの初心者で、Rails 4 を使用しています。

私のアプリケーションでは、404 と 500 のすべてのエラーを JSON 形式で返したいと考えています。

{
    "status": 404,
    "message": "not found"
}

これを行う簡単な方法はありますか?Rails 3.xでこれを行うためのソリューションを見つけただけです。

ありがとう

このソリューションを実行しようとしていますRailsでJSON形式の404エラーを返す必要がありますが、error during failsafe response: uninitialized constant ErrorsController

4

1 に答える 1

14

あなたはこれを探しているかもしれません:

render :json => @error_object.to_json, :status => :unprocessable_entity

おそらく、次のようなすべての標準エラーをキャッチできます。

class ApplicationController < ActionController::Base
  rescue_from StandardError do |exception|
    # render what you want here
  end
end
于 2013-11-09T16:55:42.643 に答える