Undefined method `paginate'
Asked Answered
F

4

9

I'm trying to use the will_paginate gem but something's wrong. I'm stuck with an undefined method `paginate' error. I read many issues and try a lot of things. Here's what I've got :

This is my LocationsController.rb:

def index
  @locations = Location.all    
  respond_to do |format|
    @locations = @locations.paginate(:page => params[:page], :per_page => 10) 
    format.html  #index.html.erb
    format.json { render json: @locations }
  end 
end

And this is my will_paginate's line in my index.html.erb:

<%= will_paginate @locations %>

And this is the error:

undefined method `paginate' for #<Class:0xaa2e48c>

I also add the require part in my Gemfile:

gem "will_paginate", "~> 3.0.4", :require => nil

And this at the end of my environment.rb:

require "will_paginate"`
Fastidious answered 14/6, 2013 at 9:49 Comment(0)
P
17

will_paginate doesn't work like that. paginate is a method of the Location class:

def index
  @locations = Location.paginate(page: params[:page], per_page: 10) 
  respond_to do |format|
    format.html
    format.json { render json: @locations }
  end 
end

Moreover, to use will_paginate you should just need to add the line below in your Gemfile, no modification in environment.rb is required:

gem "will_paginate", "~> 3.0.4" 
Pentameter answered 14/6, 2013 at 9:53 Comment(0)
K
26

Make sure to restart the server after installing the 'will_paginate' gem.

This was the stupid issue in my case.

Karlin answered 30/4, 2016 at 16:7 Comment(3)
Ironically; I've used this answer as my solution three times now over the past years. No matter what I do; I can never remember to re-start Rails S after installing the gem!Chapnick
but I am sure there must be a way not keep restarting the server after installing any gem?Beekman
Adding to the first comment, I have also used this answer as my solution twice within the last 2 months.Spoon
P
17

will_paginate doesn't work like that. paginate is a method of the Location class:

def index
  @locations = Location.paginate(page: params[:page], per_page: 10) 
  respond_to do |format|
    format.html
    format.json { render json: @locations }
  end 
end

Moreover, to use will_paginate you should just need to add the line below in your Gemfile, no modification in environment.rb is required:

gem "will_paginate", "~> 3.0.4" 
Pentameter answered 14/6, 2013 at 9:53 Comment(0)
M
3

You try to paginate array. Try this:

def index
   @locations = Location.paginate(:page => params[:page], :per_page => 10)   

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

If you want to paginate array see Paginating an Array in Ruby with will_paginate

Marybethmaryellen answered 14/6, 2013 at 9:57 Comment(0)
C
0

if anyone comes here looking for answer why <%= will_paginate %> is not working or giving an error of wrong arguments . please do this once

1- replace your gem file will paginate gem with this gem "will_paginate", "~> 3.1.7"

2- then run 'bundle install' in your console and restart server.

i hope this will help someone.

Caress answered 25/8, 2020 at 12:24 Comment(1)
i was facing same issue due to version of will_paginate gem.thats why i decided to put it in this postCaress

© 2022 - 2024 — McMap. All rights reserved.