2012-02-16 55 views
12

我有這樣的模板:如何分頁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) } 

女巫返回:

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

而且我想,爲了補充添加一些分頁PARAMS渲染這樣的:

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

任何想法?非常感謝!

+0

最好是添加頁眉分頁信息[「鏈接」]舉例 – shingara 2012-02-16 15:12:18

+0

我完全同意喜歡通過Github上做。但是現在我必須與一些當前的API保持兼容。 – 2012-02-16 16:42:03

回答

16

對不起,我的noob問題,由自述文件回答。這裏的分頁的例子:

object false 

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

child(@posts) do 
    extends "api/v1/posts/show" 
end 

注:我使用Kaminari進行分頁。

+0

當對象是一個數組時,調用'object(false)'對我來說似乎不起作用。你有沒有可能想象你的index.rabl看起來像什麼? – 2012-06-14 23:09:42

1

說明,供will_paginate 3.0.0以下工作:

node(:total) {|m| @posts.total_entries } 
node(:total_pages) {|m| (@posts.total_entries.to_f/@posts.per_page).ceil } 
node(:page_num){|m| @posts.current_page} 
+0

總計頁面計算應該是:'stuff.total_entries.to_f/stuff.per_page).ceil',以表示最後一頁未滿。 – arnab 2012-09-20 07:21:44

+0

@arnab謝謝,回答更新,以反映 – davidcollom 2013-02-07 14:56:26

3

kaminarirabl搜索這是第一次和幾乎唯一的相關結果。因此,我想根據HAL Specification在這裏留下一個解決方案,生成鏈接this

因此,首先,開始與圖:

# api/v1/posts/index.rabl 
object false 

child(@posts) do 
    extends 'api/v1/posts/show' 
end 

node(:_links) do 
    paginate @posts 
end 

然後進行定義PAGINATE方法:

# app/helpers/api_helper 
module ApiHelper 
    def paginate(collection) 
    current_page_num = collection.current_page 
    last_page_num = collection.total_pages 

    { 
     :first => first_page, 
     :previous => previous_page(current_page_num), 
     :self => current_page(current_page_num), 
     :next => next_page(current_page_num, last_page_num), 
     :last => last_page(last_page_num) 
    } 
    end 

    def first_page 
    { :href => url_for(:page => 1) } 
    end 

    def previous_page(current_page_num) 
    return nil if current_page_num <= 1 
    { :href => url_for(:page => current_page_num-1) } 
    end 

    def current_page(current_page_num) 
    { :href => url_for(:page => current_page_num) } 
    end 

    def next_page(current_page_num, last_page_num) 
    return nil if current_page_num >= last_page_num 
    { :href => url_for(:page => current_page_num+1) } 
    end 

    def last_page(last_page_num) 
    { :href => url_for(:page => last_page_num) } 
    end 
end 

最後,包括在必要的控制器輔助。助手可以包含在Api::BaseController,使所有API控制器繼承:

helper :api 

我不能這樣做,而不字形字形..的解決方案,所以...太感謝你了!

+1

,打破常規視圖分頁,因爲默認情況下,導軌包括所有助手 – 2015-02-02 15:25:56

+0

然後要麼配置Rails,因此它不包括所有助手或只是重命名'paginate'方法。 – Ashitaka 2015-02-02 15:30:24

+0

是的,只需要注意,因爲rails默認行爲與解決方案衝突 – 2015-02-02 15:32:38

0

這可能是你在找什麼;)

object false 
node :comments do 
    partial('posts/index', object: @posts) 
end 

node(:pagination) do 
    { 
    total:@posts.count, 
    total_pages: 20 
    } 
end 
相關問題