Search code examples
ruby-on-railswill-paginatekaminarirabl

How to paginate Rabl's collections


I have this template:

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

Witch returns:

{
    "posts": [
        {
            "post": {
                "id": 5,
                "title": "...",
                "subject": "...",
                "user": {
                    "full_name": "..."
                },
                "read": true
            }
        }
    ]
}

And I would like to add to add some pagination params in order to rendering this:

{
    "posts": [
        {
            "post": {
                "id": 5,
                "title": "...",
                "subject": "...",
                "user": {
                    "full_name": "..."
                },
                "read": true
            }
        }
    ],
    "total": 42,
    "total_pages": 12
}

Any ideas? Many thanks!


Solution

  • Sorry for my noob question, whitch was answered by the README. Here's an example of pagination:

    object false
    
    node(:total) {|m| @posts.total_count }
    node(:total_pages) {|m| @posts.num_pages }
    
    child(@posts) do
      extends "api/v1/posts/show"
    end
    

    Note: I'm using Kaminari for pagination.