undefined method `paginate' for Array on Rails 3 with mongoid
Asked Answered
C

4

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.

Contortionist answered 10/6, 2013 at 8:41 Comment(1)
did you ran bundle install after you added the gem to your gemfile?Lefthander
L
9

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

require 'will_paginate/array'
Lefthander answered 10/6, 2013 at 8:50 Comment(2)
Not really a tutorial, but look at this website: github.com/mislav/will_paginate/wiki/Backwards-incompatibilityLefthander
Thanks. I've created will_paginate_extensions.rb with require 'will_paginate/array' and put it under config/initializers as in https://mcmap.net/q/433499/-paginating-an-array-in-ruby-with-will_paginateContortionist
L
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'
Levorotation answered 12/6, 2013 at 12:53 Comment(0)
M
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.

Mcdougal answered 30/6, 2013 at 11:27 Comment(0)
D
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.

Dunaway answered 28/7, 2013 at 19:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.