Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

will_paginate undefined method 'per'

I recently installed the will_paginate gem to my development blog and I am having trouble. I added it to my gemfile:

gem 'will_paginate'

then fed it into the posts controller:

def index
  @posts = Post.paginate(:per_page => 5, :page => params[:page], :order => 'created_at DESC')

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

and finally into the view:

 <div id="post" style="background-color: gray; border-radius: 20px; border-bottom: solid    black 2px; padding-bottom: 40px; padding-top: 40px; margin: auto;">
    <%= post.content.html_safe %>
 </div>
<br />
<div style="padding-bottom: 40px; background-color: brown;">
    <li>Posted:&nbsp;&nbsp;<%= post.created_at.to_formatted_s(:long) %></li>
    <li>Author:&nbsp;&nbsp;<%= post.author_name %></li>
    <li>Comments:&nbsp;&nbsp;<%= post.comments.count %></li>
</div>
<br />
<br />

<%= will_paginate @posts %>

The index page works OK. It is not paginated currently because I don't have enough posts created in the development environment (was going to create them through the admin panel, ran into this). It also works fine on the Admin panel (using active_admin). It's only when I click "posts" (or any other link) on the admin panel to make a new post that I get the following error:

NoMethodError in Admin::PostsController#index
undefined method `per' for #       <ActiveRecord::Relation::ActiveRecord_Relation_Post:0x5b37d30>

I'm not really sure how to proceed, and appreciate any help. Sorry for the easy question.

like image 321
Joseph Sawyer Avatar asked Nov 30 '22 11:11

Joseph Sawyer


1 Answers

Looks like ActiveAdmin uses Kaminari for its pagination which conflicts with will_paginate.

See here for a work around: https://github.com/gregbell/active_admin/blob/47aa68d33da02c2c05cf1769402aac3df0ad02c7/docs/0-installation.md

# config/initializers/kaminari.rb
Kaminari.configure do |config|
  config.page_method_name = :per_page_kaminari
end
like image 107
Helios de Guerra Avatar answered Dec 07 '22 01:12

Helios de Guerra