0

I have 2 models docs and categories.

The relations are:

  • a doc belongs to one category.

  • a category has many docs.

I want to paginate over all docs and docs from a specific category. Here is what i did with kaminari readme in routes.rb (well, am not a rails guru yet)

   resources :docs do
     get 'page/:page', :action => :index, :on => :collection
   end
   match '/doc/',                   to: 'Docs#index',     :as => :docs

   resources :category_docs do
     get 'page/:page', :action => :category, :on => :collection
   end
   match '/doc/category/:category', to: 'Docs#category',  :as => :docs_category

The pagination for all docs works fine, but category pagination gives a RoutingError at this line in my view

   <%= paginate @category_docs, :left => 2, :right => 2 %>

Here's docs controller

   def category
     @title = "Title"
     category = nil
     if params[:category] == "a"
       category = Category.find_by_title( "aa" )
     # code omitted
     elsif params[:category] == "e"
       category = Category.find_by_title( "ff" )
   end

   if category != nil
       @category_docs = category.docs.page( params[ :page ] ).per( 10 )
   else
       @category_docs = Doc.order("updated_at DESC").page( params[ :page ] ).per( 10 )
   end

Where am i wrong? Should i switch to will_paginate? Or just throw out this code and do something else?

Dmitriy
  • 341
  • 2
  • 13

1 Answers1

0
match '/doc/category/:category', to: 'Docs#category',  :as => :docs_category

Shouldn't that be :category_docs, not docs_category? I don't know the kaminari gem in detail, but when you call paginate @category_docs, I think by default that's what it will look for.

Chris Salzberg
  • 27,099
  • 4
  • 75
  • 82