【发布时间】:2014-07-10 18:00:15
【问题描述】:
我在一个简单的 Rails API 中有以下控制器代码:
class Api::V1::AccountsController < ApplicationController
def index
render json: Account.all
end
def show
begin
render json: Account.includes(:cash_flows).find(params[:id]), include: :cash_flows
rescue ActiveRecord::RecordNotFound => e
head :not_found
end
end
end
问题在于,生成的 json 格式如下:
{
id:2,
name: 'Simple account',
cash_flows: [
{
id: 1,
amount: 34.3,
description: 'simple description'
},
{
id: 2,
amount: 1.12,
description: 'other description'
}
]
}
我需要生成的 json 是 camelCase('cashFlows' 而不是 'cash_flows')
提前致谢!!!
【问题讨论】:
-
您应该考虑从 JBuilder 开始。我强烈建议您开始使用它并利用它的功能。它将允许您更多地使用您的响应并进行更多详细的映射。你可以从这里开始:github.com/rails/jbuilder 或这里richonrails.com/articles/…
标签: ruby-on-rails ruby json api