views:

57

answers:

1

Hi,

I'm struggling to paginate using mongoid. I'm able to call paginate on my collection object like

@results = @collections.paginate :page => 1, :per_page => 10

but how it needs to be referred in view?

In my view I have rendered the collection in partial as <%= render :collection => @collections, :partial => collect.xml %> <%= will_paginate @results %>

In the above line I get the error undefined method total_pages for array.

But there is no error if I remove the will_paginate call in view and all the collections are shown in the view without pagination. Pls help.

I think I'm going wrong in calling the pagination helper in view. I have searched for quite a long time and did not find an example that included the pagination call in view for mongoid.

Forgive me if it is a dumb question. I'm new to mongoid.

A: 

This method call

@results = @collections.paginate :page => 1, :per_page => 10

will populate @results with an array of 10 items from @collections. You don't need to do use will_paginate at all, just use the @results object as is.

If you get problems with the view giving more than 10 results, debug the controller and the view to ensure there isn't something in between messing with your @results array.

EDIT:

Aha, so the problem is in displaying pagination links on the view. Yes, for that you would need the view to know things like the current page and the total number of pages available, so that you can compute how many links are necessary.

In return, your links would need to let the controller know what page is being requested, which you can achieve with GET request parameters. Helpers are probably the best way to keep the view clean.

Andres Freyria
Thanks Andres, yes I did get the 10 results displayed in the view. But my problem is displaying the pagination links in the view. So we need to build a custom helper to display the pagination links right?. I need one such thing using extjs
ljay