bluetickk
bluetickk

Reputation: 689

switch order of blog posts

I need my blog posts to display the opposite way. post 1 should be underneath post 2, etc.

This is my code which loops through the posts & renders them:

<% @posts.each do |post| %>
  <div>
    <div><%= post.name %></div>
    <div><%= post.title %></div>
    <div><%= post.content %></div>
    <div><%= link_to 'Show', post %></div>
    <div><%= link_to 'Edit', edit_post_path(post) %></div>
    <div><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></div>
  </div>
<% end %>

Upvotes: 0

Views: 482

Answers (2)

DGM
DGM

Reputation: 26979

Sort them before assigning them to @posts in the controller ...

in rails 3:

@posts = Posts.order('id DESC')

The benefit here is with a little work you can also sort by other columns too...

Upvotes: 3

mu is too short
mu is too short

Reputation: 434665

You could throw a reverse in there:

<% @posts.reverse.each do |post| %>

I'm guessing that you don't want to reverse the whole sort order because you have some pagination involved and that you want the over all order as it already is.

Upvotes: 2

Related Questions