rails 分页

基本步骤

1.安装will_paginate

编辑GemFile,添加一行:gem ‘will_paginate’

然后执行:

$  bundle install

 2. 修改config/environment.rb文件

     在config/environment.rb 文件的最后添加

require'will_paginate'

 3. 选择你要加分页的控制器(controller文件)

没加分页前的代码

def history
@score = Score.all
render  'search'
end

 添加后代码

def history
    @scores = Score.paginate :page => params[:page], :per_page => 10
    render 'search'
  end

 4. 修改viem

代码

<%= form_tag('/history') do %>
      <%= submit_tag('历史查询') %>
  <% end %>
</div>
<% if @scores != nil %>
    <table>
      <tr>
        <th>帐号</th>
        <th>姓名</th>
        <th>沟通能力</th>
        <th>职业素养</th>
        <th>学习能力</th>
        <th>演讲能力</th>
        <th>综合能力</th>
        <th>周</th>
      </tr>
      <% @scores.each do |score| %>
          <tr>
            <td><%=User.find_by_name(score.name).user_id %></td>
            <td><%= score.name %></td>
            <td><%= score.ability_to_communicate %></td>
            <td><%= score.professional_quality %></td>
            <td><%= score.ability_to_learn %></td>
            <td><%= score.speech_ability %></td>
            <td><%= score.comprehensive_ability %></td>
            <td>第<%= score.number %>周评分</td>
            <td><%= link_to '修改', edit_path(score) %></td>
          </tr>
      <% end %>
    </table>
    <%= will_paginate @scores, :previous_label => '上一页', :next_label => '下一页' %>
<% end %>

 添加了

<%= will_paginate @scores, :previous_label => '上一页', :next_label => '下一页' %>

 重启服务器

相关推荐