问题
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