简体   繁体   中英

Rails tutorial will_paginate raises undefined_method total_pages

Ive been stuck for a while now in the rails tutorial. Im in section 10.3, where we are supposed to add pagination. I have added the gem 'will-paginate' to my Gemfile, and this is the index view

<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 %>

The server raises the following problem:

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>

I have been trying to search for it.. but none of the solutions to apparently similar issues worked. Any ideas?

Just scroll lower in the tutorial - the next section addresses that problem :)

The view in Listing 10.27 doesn't work yet, though, because currently @users contains the results of User.all (Listing 10.20), which is of class Array, whereas will_paginate expects an object of class WillPaginate::Collection.

It then has you modify the UsersController to fix it-

class UsersController < ApplicationController
  before_filter :authenticate, :only => [:index, :edit, :update]
  .
  .
  .
  def index
    @title = "All users"
    @users = User.paginate(:page => params[:page])
  end
  .
  .
  .
end

我猜你错过了一个关于will_paginate的论证:

will_paginate @users

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM