Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kaminari & Rails pagination - undefined method `current_page'

People also ask

What is Denki's full name?

Denki Kaminari, professionally known as the Stun Gun Hero: Chargebolt or simply Chargebolt, is a major protagonist in the 2014 Japanese superhero manga series My Hero Academia and its 2016 anime television series adaptation of the same name.

Who is Denki's girlfriend?

MHA: Olivia Quinn (Denki's girlfriend)

Is Kaminari a UA traitor?

Kaminari's original character design originally looked a whole lot more sinister. That leaves us wondering if Kaminari was always intended to be a traitor, but ultimately he got a redesign to his ultimate betrayal would be much more shocking for the fans.

Is Kaminari Denkis last name?

Denki Kaminari ( 上 かみ 鳴 なり 電 でん 気 き , Kaminari Denki?), also known as Stun Gun Hero: Chargebolt (スタンガンヒーロー・チャージズマ, Sutan Gan Hīrō Chājizuma?), is a student in Class 1-A at U.A.


Try with:

def show
  @topic = Topic.find(params[:id])
  @posts = @topic.posts.page(params[:page]).per(2)
end

And then:

<%= paginate @posts %>

If you get pagination errors in Kaminari like

undefined method `total_pages'

or

undefined method `current_page'

it is likely because the AR scope you've passed into paginate has not had the page method called on it.

Make sure you always call page on the scopes you will be passing in to paginate!

This also holds true if you have an Array that you have decorated using Kaminari.paginate_array

Bad:

<% scope = Article.all    # You forgot to call page :(  %>
<%= paginate(scope)       # Undefined methods...        %>

Good:

<% scope = Article.all.page(params[:page]) %>
<%= paginate(scope) %>

Or with a non-AR array of your own...

Bad:

<% data = Kaminari.paginate_array(my_array)   # You forgot to call page :(        %>
<%= paginate(data)                            # Undefined methods...     %>

Again, this is good:

<% data = Kaminari.paginate_array(my_array).page(params[:page]) %>
<%= paginate(data) %>

Some time ago, I had a little problem with kaminari that I solved by using different variable names for each action.

Let's say in the index action you call something like:

def index
  @topic = Topic.all.page(params[:page])
end

The index view works fine with <%= paginate @topic %> however if you want to use the same variable name in any other action, it throu an error like that.

def list
  # don't use @topic again. choose any other variable name here
  @topic_list = Topic.where(...).page(params[:page])
end

This worked for me.

Please, give a shot.