我如何使用我拥有的数据库 table 从 rabl gem 和金钱 gem 生成 json 响应?

How can i generate json response from rabl gem and money gem with the database table i have?

这是我想从 rabl gem

构建的 json
[
"M-0": {"revenue":"value", "fee":"value", "created":"count", "paid":"count", "partially_paid":"count"},
"M-1": {"revenue":"value", "fee":"value", "created":"count", "paid":"count", "partially_paid":"count"},
"M-2": {"revenue":"value", "fee":"value", "created":"count", "paid":"count", "partially_paid":"count"},
"M-3": ["revenue":"value", "fee":"value", "created":"count", "paid":"count", "partially_paid":"count"],
"M-4": {"revenue":"value", "fee":"value", "created":"count", "paid":"count", "partially_paid":"count"},
"M-5": {"revenue":"value", "fee":"value", "created":"count", "paid":"count", "partially_paid":"count"}
 ]

这是我的数据库table

create_table "merchant_monthly_stats", force: true do |t|
    t.integer  "created_count",        default: 0
    t.integer  "ready_count",          default: 0
    t.integer  "paid_count",           default: 0
    t.integer  "partially_paid_count", default: 0
    t.integer  "cancelled_count",      default: 0
    t.integer  "failed_count",         default: 0
    t.integer  "processed_count",      default: 0
    t.integer  "expired_count",        default: 0
    t.integer  "merchant_id",                          null: false
    t.integer  "year",                                 null: false
    t.integer  "month",                                null: false
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "overdue_count",        default: 0
    t.integer  "fee_centimes",         default: 0,     null: false
    t.string   "fee_currency",         default: "XAF", null: false
    t.integer  "revenue_centimes",     default: 0,     null: false
    t.string   "revenue_currency",     default: "XAF", null: false
  end

这是我的控制器方法

def history
    current_month = Time.now.month
    current_year = Time.now.year
    list_of_last_six_months = [current_month]
    5.times do |i|
      list_of_last_six_months << (current_month - i )
    end 

    month_array =[]
    list_of_last_six_months.each { |month|
      record = MerchantMonthlyStat.find_by merchant_id: current_api_user.id, year: current_year, month: month
      m = { 
        :revenue => Money.new(record.revenue_centimes , record.revenue_currency).format,
        :fees => Money.new(record.fee_centimes,record.fee_currency).format, 
        :created => record.created_count , 
        :paid => record.paid_count , 
        :partially_paid => record.partially_paid_count }
      month_array.push(m)
    }
    respond_with(month_array, status: :ok, template: 'api/merchant/mobiles/history')
  end

我的问题是如何编写 rabl 模板 'api/merchant/mobiles/history' 以获得上述 json 响应?

注意:我正在使用 rails 钱 gem。这就是为什么有 Money.new 等

更新

如果我有以下内容:

# app/app.rb
get "/posts", :provides => [:json, :xml] do
  @user = current_user
  @posts = Post.order("id DESC")
  render "posts/index"
end

Rabl 文档为此编写了以下 rabl(这对我来说没有意义):

# app/views/posts/index.rabl
collection @posts
attributes :id, :title, :subject
child(:user) { attributes :full_name }
node(:read) { |post| post.read_by?(@user) }

但我不知道如何应用它来让我的 month_array 以 rabl 和我想要的格式出现?有人想解决这个问题吗?我卡住了。

只需使用render :json

render json: month_array

无需搞乱模板,您已经在控制器中构建了数组。

根据评论更新:使 month_array 实例可用 – 将 my_array 更改为 @my_array。然后在你的 rabl 模板中只写 collection @my_array, object_root: false