这是我想从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"}
 ]

这是我的数据库表
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 money gem。这就是为什么有钱。新的等等
更新:
如果我有:
# app/app.rb
get "/posts", :provides => [:json, :xml] do
  @user = current_user
  @posts = Post.order("id DESC")
  render "posts/index"
end

rabl docs为它写了以下rabl(这对我来说没有意义):
# app/views/posts/index.rabl
collection @posts
attributes :id, :title, :subject
child(:user) { attributes :full_name }
node(:read) { |post| post.read_by?(@user) }

但是我不知道如何应用这个来得到我想要的rabl格式的月数组?有人想解决这个问题吗?我卡住了。

最佳答案

只需使用render :json

render json: month_array

不需要破坏模板,您已经在控制器中构建了数组。
从评论更新:使month_array instance变为vairable–将my_array更改为@my_array。然后在rabl模板中只写collection@my_array,object_root:false

关于ruby-on-rails - 我如何从Rabl gem和money gem与我拥有的数据库表中生成json响应?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31809223/

10-11 03:02