Rails: Showing 10 or 20 or 50 results per page with will_paginate how to?

前端 未结 3 821
我在风中等你
我在风中等你 2021-02-06 14:58

me again...

I need show 10 or 20 or 50 results number of results per page with a select options in my list of posts using will_paginate plugin

Can you help me p

3条回答
  •  梦如初夏
    2021-02-06 15:13

    Looks like the OP also asked here: http://railsforum.com/viewtopic.php?id=33793 and got much better answers.

    To adapt the best solution there, here's what I like:

    (in the view)

    <%= select_tag :per_page, options_for_select([10,20,50], params[:per_page].to_i),
           :onchange => "if(this.value){window.location='?per_page='+this.value;}" %>
    

    (in the controller)

    @per_page = params[:per_page] || Post.per_page || 20
    @posts = Post.paginate( :per_page => @per_page, :page => params[:page])
    

提交回复
热议问题