arailsdemo x

app/models/post.rb

class Post < ActiveRecord::Base
  ...
  def post(position)
    increment = position == :previous ? -1 : 1
    Post.find_by_sequence(self.sequence + increment)
  end
end

app/helpers/post_helpers.rb

module PostsHelper
  def post_link(position)
    link_to("#{position.to_s.capitalize} Post", post_url(@post.post(position).sequence)) if @post.post(position)
  end
end

app/views/posts/show.html.haml

- title "\##{@post.sequence} #{@post.title}"
...

-# edited 12/19/10
.postNav
  .prev= post_link(:previous)
  .next= post_link(:next)
%br
%br
%p= link_to 'View All Posts', posts_path
...

你可能感兴趣的:(arailsdemo x)