Paginate Multiple Models in Kaminari

后端 未结 3 944
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-01 18:14

I\'m creating a search page that will do an application wide search on users, posts, and comments. I currently have:

# POST /search
def index
  query = params[:q         


        
3条回答
  •  难免孤独
    2021-02-01 19:05

    Ever since this commit: https://github.com/amatsuda/kaminari/commit/f9f529fb68ab89feea38773a4c625c1b14859128

    You can do the following

    In your view you can do this:

    <%= paginate @users, :remote => true, :param_name => "user_page" %>
    <%= paginate @posts, :remote => true, :param_name => "post_page" %>
    <%= paginate @comments, :remote => true, :param_name => "comment_#{some_post_id}_page" %>
    

    and then in your controller you can refer to them in this way:

    @users = User.search(query).page(params[:user_page])
    @posts = Post.search(query).page(params[:post_page])
    @comments = Comment.search(query).page(params[:comment_page])
    

    and your view's js.erb you might have something like:

    $('#posts').html('<%= escape_javascript render(@posts) %>');
    $('.table-pager').html('<%= escape_javascript(paginate(@posts, :remote => true).to_s) %>');
    

提交回复
热议问题