will_paginate是一个非常好用和常用的分页gem。那么如何在一个bootstrap风格的网页上显示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