简体   繁体   English

will_paginate undefined方法'per'

[英]will_paginate undefined method 'per'

I recently installed the will_paginate gem to my development blog and I am having trouble. 我最近在我的开发博客上安装了will_paginate gem,但我遇到了麻烦。 I added it to my gemfile: 我把它添加到我的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). 它也可以在Admin面板上正常工作(使用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. 看起来ActiveAdmin使用Kaminari进行分页,与will_paginate冲突。

See here for a work around: https://github.com/gregbell/active_admin/blob/47aa68d33da02c2c05cf1769402aac3df0ad02c7/docs/0-installation.md 请参阅此处了解解决方法: 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM