Search code examples
ruby-on-railsruby-on-rails-4ransackkaminari

Kaminari and Ransack gem


Im using the ransack gem which performs queries and shows relevant posts. Now I'm trying to implement the kaminari gem to show 12 posts per page.

here is my controller:

def index
    @search = Post.search(params[:q])
    @post = @search.result(distinct: true)

    @post = Post.order('created_at DESC').page(params[:page]).per(12)
end

the problem I have is when i click on the search button, it does not work, the posts same the same, nothing gets updated.


Solution

  • Don't run the kaminari paging on Post... that just ignores the ransack results and creates a new collection. Run paging on the @post collection.

    def index
        @search = Post.search(params[:q])
        @post = @search.result(distinct: true)
    
        @post = @post.order('created_at DESC').page(params[:page]).per(12)
    end