gpt4 book ai didi

ruby-on-rails - 如何标准化Rails项目的JSON结果

转载 作者:数据小太阳 更新时间:2023-10-29 07:28:33 26 4
gpt4 key购买 nike

我需要为 Rails 项目的 JSON 结果添加额外的标签。

获取/menus

{
meta: {
code: 200,
message: ""
}
data: [
// default rails response goes here
]
}

我不想在 Controller 中做这样的事情:

render json: { meta: { code: 200, message: ''}, data: @store.menus }

我查看了 active_model_serializers gem,但没有找到任何提供此类自定义的选项。

最佳答案

您可以创建一个 JsonResponse 类作为 View 模型来包装您要发回的数据:

class JsonResponse
attr_accessor :status, :message, :data

STATUS_SUCCESS = 200;
STATUS_CREATED = 201;
STATUS_NOT_FOUND = 404;

def self.success(data, message = nil)
self.new(STATUS_SUCCESS, message || "OK", data)
end

def self.created(data, message = nil)
self.new(STATUS_CREATED, message || "CREATED", data)
end

def self.not_found(data = nil, message = nil)
self.new(STATUS_NOT_FOUND, message || "NOT FOUND", data)
end

def initialize(status = 200, message = "", data = nil)
@status = status
@message = message
@data = data
end

def to_hash
{
meta: {
code: status,
message: message || ""
},
data: data.is_a?(Hash) ? data : data.to_hash
}
end
end

这为您提供了几种使用方法:

# One-liners
render json: JsonResponse.new(JsonResponse::STATUS_SUCCESS, nil, @store.menus).to_hash
render json: JsonResponse.success(@store.menus).to_hash
render json: JsonResponse.created(@store).to_hash
render json: JsonResponse.not_found.to_hash

# Multi-liners
response = JsonResponse.new JsonResponse::STATUS_SUCCESS, nil, @store.menus
response = JsonResponse.success @store.menus
response = JsonResponse.created @store
response = JsonResponse.not_found

# Render the JSON view
render json: response.to_hash

关于ruby-on-rails - 如何标准化Rails项目的JSON结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28154654/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com