Mongoid pagination

后端 未结 8 657
無奈伤痛
無奈伤痛 2021-02-05 12:01

I tried

@posts = Post.page(params[:page]).per_page(10)

and

@posts = Post.paginate(:page => 1, :per_page => 10)    
         


        
8条回答
  •  萌比男神i
    2021-02-05 12:45

    Still using will_paginate is also okay.

    This thread has same issue: undefined method `paginate' for Array on Rails 3 with mongoid

    The main point to fix the error is to add this line before the controller call paginate library:

    require 'will_paginate/array'
    

    It should be added to default config file if you use mongoid for the whole project.

    Hope the explanation helpful.

    Reference from origin gem source: https://github.com/mislav/will_paginate/wiki/Backwards-incompatibility at "WillPaginate::Collection" section.

    P/S: this is just a work around if your query is not very large. If you want better performance, let's try mongoid-pagination gem, custom will_pagination gem or another pagination gem which supported Mongoid like kaminari.

提交回复
热议问题