【发布时间】:2015-08-03 21:14:50
【问题描述】:
我想自定义respond_with的错误响应。它呈现错误的方式是这样的:
# /app/controllers/articles_controller.rb
def create
article = Article.new(params[:article])
article.save
respond_with(article)
end
Response:
{
errors: {
title: ["can't be blank", "must be longer than 10 characters"],
body: ["can't be blank"]
}
}
我希望它以不同的方式响应。有没有办法覆盖这种格式?
我已经通过猴子修补 ActionController::Responder 类并重新定义 json_resource_errors 成功地做到了这一点,但这似乎是一个不好的方法。
【问题讨论】: