Pagination and sorting in a Rails RESTful application

前端 未结 2 405
孤独总比滥情好
孤独总比滥情好 2021-01-03 15:40

Any tips on how to implement sorting and pagination on a resource in a Rails application and still keeping it RESTful?

How do I map the parameters for page number a

相关标签:
2条回答
  • 2021-01-03 16:28

    I would recommend you take a look at this article:

    http://dev.nozav.org/rails_ajax_table.html

    http://dev.nozav.org/ajaxtable/ (this is the demo application)

    I have used the same basic code in one of my applications. I didn't use the AJAX implementation that they describe but I used the sorting helpers that they mention and then I structured my view to enable sorting. In the sorting helpers you'll just want to take out the ajax related stuff. Mine end up looking like this:

    def sort_link_helper(text, param)
      key = param
      key += "_reverse" if params[:sort] == param
      parameters = params.merge({:sort => key, :page => params[:page], :action => controller.action_name })
      link_to(text, url_for(parameters))
    end
    
    def sort_td_class_helper(param)
      result = 'class="sortup"' if params[:sort] == param
      result = 'class="sortdown"' if params[:sort] == param + "_reverse"
      return result
    end
    

    The best part about this implementation is that is RESTful, DRY and easy to use!

    0 讨论(0)
  • 2021-01-03 16:33

    Have you tried will_paginate? Is there some reason it doesn't meet your specific needs? (http://github.com/mislav/will_paginate/tree/master)

    0 讨论(0)
提交回复
热议问题