Search code examples
rubyruby-on-rails-3will-paginate

"Undefined method paginate" error message is coming while using will_paginate Gem


I am getting the following error when i am implementing will_paginate gem in my app.

Error:

NoMethodError in ProductsController#index

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

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

Please check my below code and please try to help me to resolve this.

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'

views/products/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 %>

controller/products_controller

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

Please try to help me.


Solution

  • Ah, just noticed you're on rails 3. In that case, try this instead:

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

    The difference is the lack of #all, which returns an Array on rails 3.