我怎么能用我的数据库表从rabl gem和money gem生成json响应?

时间:2015-08-04 12:20:39

标签: ruby-on-rails ruby json currency rabl

这是我想用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。这就是Money.new等的原因

更新

如果我有以下内容:

# 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) }

但我不知道如何应用这个来获取我的month_array在rabl和我想要的格式?有人想解决这个问题吗?我被卡住了。

1 个答案:

答案 0 :(得分:1)

只需使用render :json

即可
render json: month_array

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

从评论中更新:将month_array实例设为可变 - 将my_array更改为@my_array。然后在你的rabl模板中写入集合@my_array,object_root:false