6

我在rails中很新,并且使用rails 4。

在我的应用程序中,我想返回格式为 JSON 的所有 404 和 500 错误

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

有一种简单的方法可以做到这一点吗?因为我只是找到了使用 rails 3.x 的解决方案。

谢谢

我试图做这个解决方案需要在 Rails 中返回 JSON 格式的 404 错误,但我得到了error during failsafe response: uninitialized constant ErrorsController

4

1 回答 1

14

May be you're looking for this:

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

And probably you may catch all standard errors like this:

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