---
title: "rails kaminari bootstrap-kaminari-views certified"
author: "暮雨风吹"
date: "2014-04-15T12:04:00Z"
tags: []
slug: "rails-kaminari-bootstrap-kaminari-views-certified"
draft: false
lastmod: "2014-04-15T14:02:16Z"
id: 2046289
url: "https://dreamskyforjava.iteye.com/blog/2046289"
---

kaminari是一个基于范围和驱动的清洁的、强大的、可定制的并且复杂的现代Web应用程序框架和对象关系模型。它只请求当前页所需的数据，不会将表中所有数据加载完然后分页（很遗憾wice\_grid就是这样的，据我所知），极大地提高了数据量大的应用的性能。

易用：

只需安装gem文件，然后你的model就可以分页了，不需要任何配置，也不必在你的models或helpers中定义任务东西。  
基于I18N的可定制引擎：  
由于所有的分页帮助都是基于链接和非链接的容器，Kaminari在自己的引擎内部模板参考了他们，因此，你能很容易的修改  
他们的行为、风格、或者重载模板的任何事情。

1.在gemfile文件中引入

```
#分页插件
gem 'kaminari'
gem 'bootstrap-kaminari-views'
```

2.执行bundle install

3.生成配置文件（这不是必须的，完全可以使用默认的，也可自己在程序中通过参数进行控制）

```
rails g kaminari:config
```

```
Kaminari.configure do |config|
  # config.default_per_page = 25
  # config.max_per_page = nil
  # config.window = 4
  # config.outer_window = 0
  # config.left = 0
  # config.right = 0
  # config.page_method_name = :page
  # config.param_name = :page
end
```

4.修改models/book.rb文件

```
class Book < ActiveRecord::Base
  #附件
  has_many :attachments, as: :owner, dependent: :delete_all, autosave: true
  has_many :assets, through: :attachments
  accepts_nested_attributes_for :assets, allow_destroy: true
  accepts_nested_attributes_for :attachments, allow_destroy: true

  paginates_per 2             #每页显示两条数据

end
```

5.修改books\_controller.rb文件

```
# GET /books
  # GET /books.json
  def index
    @books = Book.order(:id).page params[:page]
  end
```

6.修改views/books/index.html.erb文件

```
<h1>Listing books</h1>

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Author</th>
      <th>Content</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>

  <tbody>
    <% @books.each do |book| %>
      <tr>
        <td><%= book.name %></td>
        <td><%= book.author %></td>
        <td><%= book.content %></td>
        <td><%= link_to 'Show', book %></td>
        <td><%= link_to 'Edit', edit_book_path(book) %></td>
        <td><%= link_to 'Destroy', book, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>

  </tbody>

</table>
<%= paginate @books %>
<br>

<%= link_to 'New Book', new_book_path %>
```

7.使用bootstrap的theme渲染kaminari分页插件

8.执行命令

```
rails g kaminari:views bootstrap
```

9.出现错误

```
 SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B:
```

```
 certificate verify failed (OpenSSL::SSL::SSLError)
```

10.解决办法，在gemfile文件中添加

```
#自定义分页插件主题
#执行rails g kaminari:views bootstrap 时报错 作用：Ensure net/https uses OpenSSL::SSL::VERIFY_PEER to
#verify SSL certificatesand provides certificate bundle in case OpenSSL cannot find one
gem 'certified'
```

11.执行bundle install

12.执行命令，生成kaminari 的view模板

```
rails g kaminari:views bootstrap
```

13.启动程序，查看效果

![](http://img.blog.csdn.net/20140415115500078)

14.更多信息请参考

[kaminari](https://github.com/amatsuda/kaminari) [bootstrap-kaminari-views](https://github.com/matenia/bootstrap-kaminari-views) [certified](https://github.com/stevegraham/certified)

15.项目源码

[liwenjuan](http://pan.baidu.com/s/1pJJg12z)