Search code examples
ruby-on-railsruby-on-rails-3paginationrubygemskaminari

Rails 3 & Kaminari pagination problem


Ok so I have decided to use Kaminari for pagination in a rails 3 project. I have followed the video from RailsCasts http://railscasts.com/episodes/254-pagination-with-kaminari

All goes well up until the point or running the server.

controllers/stories_controller.rb

def index
   @stories = Story.all
   @pages = Story.page(params[:page]).per(3)
   @stories = Story.search(params[:search]) 
end

views/stories/index.html.erb

<%= paginate @pages %>

When i start the server the index page in question displays all the stories from the DB and renders the pagination view showing (1 2 Next > Last »). What am I missing to get the pagination working?


Solution

  • I still can not understand your code. Why do you assign Story.all to @stories in the 1st line and overwrite the variable in the 3rd line?

    Anyways, @stories will display "all the stories from the DB" because you're not calling the pagination method (.per) on @stories. The pagination links will show you the paginated counts because you're calling per method on @page variable and passing it to the helper.

    I mean, you need to call .per on the relation before passing it to <%= paginate %> helper. It's quite simple.