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
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!
Have you tried will_paginate? Is there some reason it doesn't meet your specific needs? (http://github.com/mislav/will_paginate/tree/master)