Return to Snippet

Revision: 39201
at January 14, 2011 15:39 by gmericksen


Initial Code
def previous_post
  self.class.first(:conditions => ["title < ?", title], :order => "title desc")
end

def next_post
  self.class.first(:conditions => ["title > ?", title], :order => "title asc")
end
You can then link to those in the view.

<%= link_to("Previous Post", @post.previous_post) if @post.previous_post %>
<%= link_to("Next Post", @post.next_post) if @post.next_post %>

Initial URL
http://stackoverflow.com/questions/1275963/rails-next-post-and-previous-post-links-in-my-show-view-how-to

Initial Description
If each title is unique and you need alphabetical, try this in your Post model. You can change title to any unique attribute (created_at, id, etc.) if you need a different sort order.

Initial Title
Previous and Next Links

Initial Tags
rails

Initial Language
Rails