这是我第一次使用rabl和gon宝石。我只是试图使用rabl输出查询结果。不幸的是,我没有得到适当的物品。
我的控制器:
@test = Category.joins('LEFT OUTER JOIN incomes ON incomes.category_id = categories.id AND incomes.dept_id = 86').group_by { |category| category.cat_ds }
我的兔子模板:
collection @test
attributes :cat_code, :cat_ds
我希望得到的结果:
{
"REVENUE": [
{
"cat_code": 10001,
"cat_ds": "REVENUE",
"id": 1,
"incomes": [
{
"category_id": "1.0",
"chart_id": 1,
"created_at": "2013-01-15T16:43:52Z",
"dept_id": 4,
"feb": "11.0",
"id": 1,
"jan": "12.12",
"note": "Testing",
"updated_at": "2013-01-15T16:43:52Z",
"year_id": 1,
"total_cost": 23
}
]
}
],
"EXPENSE": [
{
"cat_code": 10002,
"cat_ds": "EXPENSE",
"id": 2,
"incomes": [
{
"category_id": "2.0",
"chart_id": 2,
"created_at": "2013-01-15T16:43:52Z",
"dept_id": 86,
"feb": "45.0",
"id": 3,
"jan": "60.0",
"note": "Two",
"updated_at": "2013-01-15T16:43:52Z",
"year_id": 1,
"total_cost": 105
},
{
"category_id": "2.0",
"chart_id": 2,
"created_at": "2013-01-15T16:43:52Z",
"dept_id": 86,
"feb": "45.55",
"id": 5,
"jan": "3454.0",
"note": "One",
"updated_at": "2013-01-15T16:43:52Z",
"year_id": 1,
"total_cost": 3499
}
]
}
]
}
我删除了上面的大部分结果。
我得到的结果:
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": [
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
}
]
}
因此,对于像Revenue and Expense这样的每个组,它返回一条记录,同样的记录。为什么它根本不解析我给它的实例变量。在呈现数据之前,它还对数据做了什么。
另外值得注意的是,如果我跳过rabl模板并使用gon中的变量,它会完美显示。那么它必须与我的rabl模板有关吗?
修改
我想看看下面是否有效,但是我收到以下错误:
Cannot find rabl template 'income/index' within registered ([]) view paths!
def index
@test_obj = Category.joins('LEFT OUTER JOIN incomes ON incomes.category_id = categories.id AND incomes.dept_id = 86').group_by { |category| category.cat_ds }
@test = Rabl::Renderer.json(@test_obj, 'income/index')
gon.rabl "app/views/incomes/index.rabl", as: "test"
end
答案 0 :(得分:1)
在您的控制器中尝试此操作。 Rabl用于渲染,所以如果你想用它来传递给gon,我认为你必须手动渲染它,因为你将为请求呈现HTML。
@test_obj = Category.joins('LEFT OUTER JOIN incomes ON incomes.category_id = categories.id AND incomes.dept_id = 86').group_by { |category| category.cat_ds }
@test = Rabl::Renderer.json(@test_obj, 'test/show')