【问题标题】:How to render error codes in Rails JSON API如何在 Rails JSON API 中呈现错误代码
【发布时间】:2018-03-27 02:45:02
【问题描述】:

我想在我的错误响应中呈现错误代码,因为 JSON API 描述了here。我使用ActiveInteraction gem,它在后台使用ActiveModel::Errors 来创建错误对象。根据documentation ActiveModel::Errors 库,如果您想向响应对象添加错误,只需设置一个属性和一条消息。

add(attribute, message = :invalid, options = {})

那么如何在添加错误的同时设置code 属性?

【问题讨论】:

    标签: ruby-on-rails ruby api json-api


    【解决方案1】:

    以下是 rails api 文档的摘录:

    person.errors.add(:name, :not_implemented, message: "must be implemented")
    

    其实 ':not_implemented' 就是你需要的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-06
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      • 2019-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多