如何对Rabl的藏品进行分页

Zag*_*g.. 12 ruby-on-rails will-paginate kaminari rabl

我有这个模板:

# app/views/posts/index.rabl
collection @posts => :posts
attributes :id, :title, :subject
child(:user) { attributes :full_name }
node(:read) { |post| post.read_by?(@user) }
Run Code Online (Sandbox Code Playgroud)

女巫回归:

{
    "posts": [
        {
            "post": {
                "id": 5,
                "title": "...",
                "subject": "...",
                "user": {
                    "full_name": "..."
                },
                "read": true
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我想添加一些分页参数来呈现这个:

{
    "posts": [
        {
            "post": {
                "id": 5,
                "title": "...",
                "subject": "...",
                "user": {
                    "full_name": "..."
                },
                "read": true
            }
        }
    ],
    "total": 42,
    "total_pages": 12
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?非常感谢!

Zag*_*g.. 16

对不起我的noob问题,自述文件由README回答.这是一个分页的例子:

object false

node(:total) {|m| @posts.total_count }
node(:total_pages) {|m| @posts.num_pages }

child(@posts) do
  extends "api/v1/posts/show"
end
Run Code Online (Sandbox Code Playgroud)

注意:我正在使用Kaminari分页.