2015-04-02 51 views
0

我在我的應用程序中實現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 

請儘量幫助我。

回答

2

啊,剛纔注意到您是在軌道3.在這種情況下,試試這個來代替:

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

的差異是缺乏#all,返回軌道上的Array 3.

相關問題