0

我在 Rails 教程中被困了一段时间。我在第 10.3 节,我们应该在其中添加分页。我已将 gem 'will-paginate' 添加到我的 Gemfile 中,这是索引视图

<h1>All users</h1>
<%= will_paginate %>
<ul class="users">
<% @users.each do |user| %>
    <li>
        <%= gravatar_for user, :size => 30 %>
        <%= link_to user.name, user %>
    </li>
<% end %>
</ul>
<%= will_paginate %>

服务器提出以下问题:

ActionView::Template::Error (undefined method `total_pages' for #<Array:0xa0283fc>):
    1: <h1>All users</h1>
    2: <%= will_paginate %>
    3: <ul class="users">
    4:  <% @users.each do |user| %>
    5:      <li>

我一直在尝试寻找它..但显然类似问题的解决方案都没有奏效。有任何想法吗?

4

2 回答 2

2

只需在教程中向下滚动 - 下一节将解决该问题 :)

不过,清单 10.27 中的视图还不能工作,因为当前 @users 包含 User.all 的结果(清单 10.20),它属于 Array 类,而 will_paginate 需要一个 WillPaginate::Collection 类的对象。

然后它让你修改 UsersController 来修复它-

class UsersController < ApplicationController
  before_filter :authenticate, :only => [:index, :edit, :update]
  .
  .
  .
  def index
    @title = "All users"
    @users = User.paginate(:page => params[:page])
  end
  .
  .
  .
end
于 2012-01-18T16:13:09.237 回答
0

我猜你错过了一个论点will_paginate

will_paginate @users
于 2012-01-18T16:12:39.377 回答