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: <%= post.created_at.to_formatted_s(:long) %></li>
<li>Author: <%= post.author_name %></li>
<li>Comments: <%= 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With