少於 1 分鐘閱讀

Github

will_paginate 這是原版的,轉為 html code 時會用 div 包,需要另外自行套樣式

will_paginate-bootstrap 如果用 bootstrap 樣式,直接使用這個即可

使用方式

controller 中

原本寫法:

@posts = Post.all

改寫為:

@posts = Post.paginate(:page => params[:page])

在template中

加入: 原版:

<%= will_paginate @posts %>

bootstrap版:

<%= will_paginate @posts, renderer: BootstrapPagination::Rails %>

其他參數

手動設定一頁的顯示數量:

@posts = Post.paginate(:page => params[:page], :per_page => 30)

該model中都用同樣的顯示數量:

class Post
  self.per_page = 10
end

設定全域頁數:

WillPaginate.per_page = 10

參考

  1. https://github.com/mislav/will_paginate
  2. https://github.com/bootstrap-ruby/will_paginate-bootstrap

留言