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
solution
map.resources :post_categories do |post_category|
post_category.resources :posts
end
map.resources :posts
Had to declare the resource after the block
Thanks stephen!
Parachuting in here on this old question because I encountered the same issue.
I had pretty much exactly the same problem. I followed the responders’ advice that something funky was going on with the routes. I dug into the routes and found (translated to suit your situation):
This made it so that calling
category_posts_pathreturned (as expected)/category/1/posts.However, it’s of pivotal importance to realize that
will_paginatecalls something that resemblesurl_for, which works “backwards” from the routes to find the first route that matches the parameters.Since
resources :postsappears above the nested route, it sees that that one satisfies the requirements and just insertscategory_id=1was a query string.The reason it worked “out of the box” for everyone else was because they didn’t have the nested resource separately listed as a standalone resource.
Remove that and you should be fine!