will_paginate是一個非常好用和常用的分頁gem。那么如何在一個bootstrap風格的網(wǎng)頁上顯示will_paginate呢?
當然可以手動自己寫樣式,也可以用下面的方式:
gem 'will_paginate-bootstrap'
bundle install
controller中:
@blogs = current_user.blogs.paginate(:page => params[:page], :per_page => 5).order("created_at desc")
views中:
<%= will_paginate @blogs, renderer: BootstrapPagination::Rails %>
在config/initializers文件夾下,新建一個will_paginate.rb的文件,添加:
WillPaginate::ViewHelpers.pagination_options[:previous_label ] = "前一頁"
WillPaginate::ViewHelpers.pagination_options[:next_label ] = "后一頁"
這樣,頁面中就會顯示成中文的的上一頁,下一頁了。
參考:
https://github.com/mislav/will_paginate
https://github.com/bootstrap-ruby/will_paginate-bootstrap