使用will_paginate时出现“未定义方法分页”错误消息Gem

问题描述:

我在我的应用程序中实现will_paginate gem时出现以下错误。使用will_paginate时出现“未定义方法分页”错误消息Gem

错误:

NoMethodError in ProductsController#index 

undefined method `paginate' for #<Array:0x2d25ae8> 

app/controllers/products_controller.rb:3:in `index' 

请检查我下面的代码,并请尽量帮我解决这个问题。

的Gemfile

source 'https://rubygems.org' 

gem 'rails', '3.2.19' 

# Bundle edge Rails instead: 
# gem 'rails', :git => 'git://github.com/rails/rails.git' 

gem 'sqlite3' 


# Gems used only for assets and not required 
# in production environments by default. 
group :assets do 
    gem 'sass-rails', '~> 3.2.3' 
    gem 'coffee-rails', '~> 3.2.1' 

    # See https://github.com/sstephenson/execjs#readme for more supported runtimes 
    # gem 'therubyracer', :platforms => :ruby 

    gem 'uglifier', '>= 1.0.3' 
end 

gem 'jquery-rails' 

# To use ActiveModel has_secure_password 
# gem 'bcrypt-ruby', '~> 3.0.0' 

# To use Jbuilder templates for JSON 
# gem 'jbuilder' 

# Use unicorn as the app server 
# gem 'unicorn' 

# Deploy with Capistrano 
# gem 'capistrano' 

# To use debugger 
# gem 'debugger' 
gem 'will_paginate', '~> 3.0' 

的意见/产品/ index.html.erb

<%= will_paginate @products %> 
<table style="width:500px"> 
    <thead> 
    <tr> 
    <th>Name</th> 
    <th style="text-align:right;">Price</th> 
    <th style="text-align:right;">Quantity</th> 
    </tr> 
    </thead> 
    <tbody> 
    <% @products.each do |product| %> 
     <tr> 
     <td><%= product.name %></td> 
     <td style="text-align:right;"><%= number_to_currency product.price %></td> 
     <td style="text-align:right;"><%= product.quantity %></td> 
     </tr> 
    <% end %> 
    </tbody> 
</table> 
<%= will_paginate @products %> 

控制器/ products_controller

class ProductsController < ApplicationController 
    def index 
    @products = Product.all.paginate(page: params[:page], per_page: 5) 
    end 
end 

请尽量帮助我。

啊,刚才注意到您是在轨道3.在这种情况下,试试这个来代替:

@products = Product.paginate(page: params[:page], per_page: 5) 

的差异是缺乏#all,返回轨道上的Array 3.