Getting will_paginate to reverse the per-page order

一世执手 提交于 2019-12-12 04:51:03

问题


I'm trying to get will_paginate to order my posts so that the newest post appears at the top of the first page but I can't seem to figure out how to get it to do that.

This is the order when I sorty by ...order(:published_at)

Page 1

2014-08-05
2014-08-04
2014-08-03
2014-08-02
2014-08-01 <- Oldest post

Page 2

2014-08-10 <- Newest post
2014-08-09
2014-08-08
2014-08-07
2014-08-06

And this is what it does when I ...order("published_at DESC") it

Page 1

2014-08-06
2014-08-07
2014-08-08
2014-08-09
2014-08-10 <- Newest post

Page 2

2014-08-01 <- Oldest post
2014-08-02
2014-08-03
2014-08-04
2014-08-05

But this is the order I actually want:

Page 1

2014-08-10 <- Newest post
2014-08-09
2014-08-08
2014-08-07
2014-08-06

Page 2

2014-08-05
2014-08-04
2014-08-03
2014-08-02
2014-08-01 <- Oldest post

回答1:


Unfortunately, in my case, I am my own worst enemy.

This was my code in index.html.erb

<% @blog_posts.reverse.each do |blog_post| %>
  #            ^^^^^^^
  <%= render "blog_post", blog_post: blog_post %>
<% end %>

and this should be the code in my index.html.erb

<% @blog_posts.each do |blog_post| %>
  <%= render "blog_post", blog_post: blog_post %>
<% end %>

and in my controller

def index
  @blog_posts = BlogPost.
    published.
    page(params[:page]).
    order("published_at DESC")
end

Produces this order

Page 1

2014-08-10 <- Newest post
2014-08-09
2014-08-08
2014-08-07
2014-08-06

Page 2

2014-08-05
2014-08-04
2014-08-03
2014-08-02
2014-08-01 <- Oldest post


来源:https://stackoverflow.com/questions/25528112/getting-will-paginate-to-reverse-the-per-page-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!