time time - 2 months ago 14
Ruby Question

Kaminari & Rails pagination - undefined method `current_page'

I searched and searched, but nothing solved my problem. Here's my controller:

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


That functions just fine, because the topic view is reduced to two posts. However, when I add this to show.html.erb:

<%= paginate @topic.posts %>


I'm given this error:

undefined method `current_page' for #<ActiveRecord::Relation:0x69041c9b2d58>

Answer

Try with:

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

And then:

<%= paginate @posts %>