gpt4 book ai didi

ruby-on-rails - 我如何使用我拥有的数据库表从 rabl gem 和 money gem 生成 json 响应?

转载 作者:太空宇宙 更新时间:2023-11-03 16:22:12 25 4
gpt4 key购买 nike

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

这是我的 Controller 方法

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 文档为此编写了以下 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 中获取我的 month_array?有人想解决这个问题吗?我卡住了。

最佳答案

只需使用render :json

render json: month_array

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

评论更新:使 month_array 实例可用——将 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/

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