views:

149

answers:

2

I'm trying to get will paginate to link to my nested route instead of the regular posts variable. I know I'm supposed to pass some params to paginate but I don't know how to pass them.

Basically there is an array stored in @posts and the other param paginate has access to is category_id.

The nested route is /category/1/posts but hitting next and previous on will paginate returns a url like this posts?page=1&category_id=7.

 <%= will_paginate @most_recent_posts "What do I do here?" %>

This is the result of Yannis's answer:

In your controller you can do:

@posts = @category.posts.paginate

And in your view:

<%= will_paginate(@post) %>

Doing this comes up with the following URL

posts?page=2&post_category_id=athlete_management

routes.rb #there are more routes but these are the relevant ones

map.resources :posts
map.resources :post_categories, :has_many => :posts
+1  A: 

In your controller you can do:

@posts = @category.posts.paginate

And in your view:

<%= will_paginate(@post) %>
Yannis
Check the question for new code
Sam
+1  A: 

Okay, so it's not clear from the question exactly, but I'm assuming that you are trying to go from a root-level URL like /posts into pages for specific categories like /category/1/posts.

The reason I assume this is because I have nested routes (in a namespace even) where will_paginate works fine without any special params. If that is not the case then there is probably something else funky going on with your routes and more information will be needed to debug.

However I can at least explain how will_paginate works. When it's generating the page URLs it calls url_for with existing params plus the specific page needed. Therefore, if you already have a bunch of params set, it should preserve them, including ones that are part of the path itself. Fortunately it also allows you to pass additional params to merge in with the (surprise!) :params parameter to will_paginate.

Now assuming you have the nested routes set up properly, /category/1/posts can be generated by:

url_for(:controller => 'posts', 
        :category_id => '1')

But if you are on the page /posts then the params will only be:

{:controller => 'posts'}

So you need to pass the category_id to will_paginate like so:

<%= will_paginate @posts, :params => {:category_id => @posts.first.category_id} %>

If this doesn't work, then I suspect something wrong with your routes. Try putting a debug statement in the template and calling url_for with different permutations of the params. Remember that the route definition order matters and could be creating conflicts.

dasil003
`/category/1/posts/page=2` that is the way I want the url to look when I paginate through the posts in the category.
Sam
Did you read what I said? Did you try it? You didn't respond to my questions or indicate anything. No one is going to give you more than I have. If it doesn't work and you need more help then you're going to have to post more information.
dasil003
you didn't explicitly say that you have pagination working with nested routes. such that it creates the url I'm trying to achieve. Have you actually paginated the category/1/posts/ url and still get that url but with the page appended to the end?
Sam
Yes I did explicitly state it, that's what I meant by "The reason I assume this is because I have nested routes (in a namespace even) where will_paginate works fine without any special params."
dasil003
and btw you mean /category/1/posts?page=2 right?
dasil003
yes, have the same url and then the page at the end
Sam
I was referring to the fact that you used a slash instead of a question mark before page. The information I've given you is the best you're going to get. From here on out you need to debug it yourself or else post more detailed information.
dasil003