Paginate single items


#1

Hi,

In my blog i display a post. I want to display a link tot he previous
post if there is 1 and a link tot he next post if there is one.

So i have in my controller

def post
@post = Post.find(params[:id])
@post_pages, @posts = paginate :posts, :per_page => 1
render(:layout => false)
end

And the following code in post.rhtml

<% if @post_pages.current.next %>
<%= link_to ‘nextitem’, :action => “post”, :id =>
@post_pages.current.next %>
<% end %>

<%= render :partial => “post”, :object => @post %>

I know the above code is wrong but that is sort of what i am looking
for.

Anyone help?


#2

John B. wrote:

Hi,

In my blog i display a post. I want to display a link tot he previous
post if there is 1 and a link tot he next post if there is one.

So i have in my controller

def post
@post = Post.find(params[:id])
@post_pages, @posts = paginate :posts, :per_page => 1
render(:layout => false)
end

And the following code in post.rhtml

<% if @post_pages.current.next %>
<%= link_to ‘nextitem’, :action => “post”, :id =>
@post_pages.current.next %>
<% end %>

<%= render :partial => “post”, :object => @post %>

I know the above code is wrong but that is sort of what i am looking
for.

Anyone help?

What you’ve included in your code is pretty close to what you need.
Just use the paginator method with :per_page set to 1, and then include
the boilerplate previous and next links in your view:

<%= link_to ‘Previous page’, { :page => @post_pages.current.previous }
if @post_pages.current.previous %>
<%= link_to ‘Next page’, { :page => @post_pages.current.next } if
@post_pages.current.next %>

Have you tried implementing it and found problems?

Jeff C.man


#3

When i click the next item it is displaying the same post but with a url
of http://localhost:3000/blog/post/19?page=2

I am not using pagination to display the first post, i am using the
render partial “post” below in post.rhtml below.

<%= link_to ‘Previous page’, { :page => @post_pages.current.previous }
if @post_pages.current.previous %>
<%= link_to ‘Next page’, { :page => @post_pages.current.next } if
@post_pages.current.next %>

<%= render :partial => “post”, :object => @post %>