简体   繁体   English

Kaminari和Ransack宝石

[英]Kaminari and Ransack gem

Im using the ransack gem which performs queries and shows relevant posts. 我使用ransack gem执行查询并显示相关帖子。 Now I'm trying to implement the kaminari gem to show 12 posts per page. 现在我正在尝试实现kaminari gem每页显示12个帖子。

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. 我遇到的问题是,当我点击搜索按钮时,它不起作用,帖子相同,没有任何更新。

Don't run the kaminari paging on Post ... that just ignores the ransack results and creates a new collection. 不要在Post上运行kaminari分页...只是忽略了搜索结果并创建了一个新的集合。 Run paging on the @post collection. @post集合上运行分页。

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

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

So i figured out the answer, rather than use the kaminari gem , i switched over to will_paginate gem and changed my controllers index method to: 所以我想出了答案,而不是使用kaminari gem ,我切换到will_paginate gem并将我的控制器索引方法更改为:

@search = Post.search(params[:q])
@posts = @search.result(distinct: true).paginate(page: params[:page], per_page: params[:per_page])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM