简体   繁体   English

Ruby on Rails:will_paginate无法正常工作

[英]Ruby on Rails: will_paginate is not working correctly

If I click the next or previous link it doesn't go to the next or previous page. 如果我单击下一个或上一个链接,则不会转到下一个或上一个页面。

All posts are on same page, but there are links next, previous at bottom. 所有帖子都在同一页面上,但是接下来的链接位于底部。

In PostsController: 在PostsController中:

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

in posts/index: 在帖子/索引中:

<%= will_paginate @posts%>

Where is the problem with will_paginate? will_paginate的问题在哪里?

您必须先定购才能进行分页,因此将其更改为

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

更新为will_paginate , '3.1.7'解决了我的问题

Not sure if this is causing your error, but ordering should be done outside of will_paginate. 不知道这是否引起您的错误,但是应该在will_paginate之外进行订购。

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

This is how it should be done in Rails 3. 这就是在Rails 3中应该如何做。

I've also had trouble setting the per_page parameter within the controller. 我也无法在控制器内设置per_page参数。 You could try setting it in the model instead. 您可以尝试在模型中进行设置。

class Post
  self.per_page = 10
end

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

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