Forum: Ruby on Rails Paginate single items

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0de83fd56f1af530034ba8efa5490b1b?d=identicon&s=25 John Butler (Guest)
on 2006-04-07 16:47
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?
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-07 17:06
John Butler 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 Coleman
0de83fd56f1af530034ba8efa5490b1b?d=identicon&s=25 John Butler (Guest)
on 2006-04-07 17:18
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 %>
This topic is locked and can not be replied to.