Somesh
Somesh

Reputation:

Pagination and table sorting in Rails

I am using will_paginate and sort helper for pagination and sorting resp.. But im facing one problem while sorting that when im on page 2 or 3 or any other page than first page. It redirects to the first page and sorts the first page only. please help me how to sort all the records and go back to page where I was.

Upvotes: 0

Views: 5397

Answers (2)

mynameisrufus
mynameisrufus

Reputation: 81

I have more powerful solution for this: https://github.com/mynameisrufus/sorted

Upvotes: 1

Irukandji
Irukandji

Reputation: 1198

What's the reason you need to go back to the page (2 or 3)? Records will change position and probabily page so you will not find them at the same place.

so why you don't change only the :order value (ex. from 'firstname DESC' to 'lastname DESC) when you call the action again?

# people_controller.rb
def index
  @people = People.search(params[:my_order], params[:page])
end

# models/people.rb
def self.search(my_order, page)
  paginate :per_page => 10, :page => page,
           :conditions => ['job like ?', "Driver"],
           :order => "%#{my_order}%"
end

Upvotes: 2

Related Questions