1

这是我第一次使用 rabl 和 gon gem。我只是尝试使用 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 }

我的 rabl 模板:

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\">]": {}
            }
        ]
    }

因此,对于像收入和费用这样的每个组,它返回一个记录,相同的记录。为什么它根本不解析我给它的实例变量。在呈现数据之前,它还对数据做了什么。

另外值得注意的是,如果我跳过 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
4

1 回答 1

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')
于 2013-01-18T01:11:36.613 回答