I have the following in a controller to enable will_paginate sorting:
def sort_column
['name', 'scheduled', 'status'].include?(params[:sort]) ? params[:sort] : "scheduled"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
end
These are supplemented by <%= sortable "status", "Status" %> at the top of a table column – clicking this will sort the table.
How can I make some columns default to ASC and some default to DESC?
Rationale
A text-based column works very well under ASC sorting. However, where I have columns with percentages or numbers, sometimes I want to show the best/highest first instead of requiring that the user click twice to get the column sorted the way they want.
Figured it out!
This was the original
application_helper.rbcode from Railscasts #228 and #240.I changed it to (changes on lines 1 and 4):
This enables me to now specify a default direction in a column heading: