will paginate, nested routes, ruby, rails

Posted by Sam on Stack Overflow See other posts from Stack Overflow or by Sam
Published on 2010-10-23T18:07:21Z Indexed on 2011/01/04 22:54 UTC
Read the original article Hit count: 374

Filed under:
|
|

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!

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby