"未定义的方法paginate"使用will_paginate Gem时出现错误消息

时间:2015-04-02 06:00:50

标签: ruby ruby-on-rails-3 will-paginate

当我在我的应用中实现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

请尽力帮助我。

1 个答案:

答案 0 :(得分:2)

啊,只是注意到你在轨道上3.在这种情况下,试试这个:

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

不同之处在于缺少#all,它会在导轨3上返回Array