1

Trying to use will_paginate gem.

My Gem file has:

gem 'will_paginate', '~> 3.0.0'

My orders_controllers.rb:

def index
    @orders = Order.all.paginate(:page => params[:page], :per_page => 20)

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @orders }
    end
  end

On my index.html.erb I've put:

<%= will_paginate @orders %>

Error:

NoMethodError in OrdersController#index

undefined method `paginate' for #<Array:0x007f79a1be62f8>
Rails.root: /home/askar/Dropbox/rails_studio/somics

Application Trace | Framework Trace | Full Trace
app/controllers/orders_controller.rb:5:in `index'

I now there's kaminari gem for pagination, but I want to know about will_paginate.

Askar
  • 5,784
  • 10
  • 53
  • 96

4 Answers4

9

Array functions are disabled by default you have to enable them by requiring them in your WillPaginate file:

require 'will_paginate/array'
Kees Sonnema
  • 5,759
  • 6
  • 51
  • 106
  • Not really a tutorial, but look at this website: https://github.com/mislav/will_paginate/wiki/Backwards-incompatibility – Kees Sonnema Jun 10 '13 at 08:56
  • Thanks. I've created will_paginate_extensions.rb with require 'will_paginate/array' and put it under config/initializers as in http://stackoverflow.com/a/13187461/1745902 – Askar Jun 10 '13 at 09:04
2

In your config/initializers add will_paginate_array_fix.rb file and in will_paginate_array_fix.rb file add

require 'will_paginate/array'
0

I just wanted to add that after you do as the previous answer suggests:

In your config/initializers add will_paginate_array_fix.rb file and in will_paginate_array_fix.rb file add

require 'will_paginate/array'

You need to restart your server in order for that to take effect.

Ahmed Fathy
  • 529
  • 4
  • 20
0

I don't think we need to add any files in the config/initializers. Just after doing all the steps as mentioned on the official github page restart the rails server and all will work fine.

mrudult
  • 2,480
  • 3
  • 35
  • 54