Table sorting

Ahoy,

I checked the API and did some forum searches but didn’t find anything
good on table sorting.

Is there a “railish” way to do this? My current method really is lacking
(i’m on like rails day 2)

<th><%= link_to 'Name', :action => 'list', :order => 'name' %></th>
<th><%= link_to 'Note', :action => 'list', :order => 'note' %></th>
<th><%= link_to 'Created on', :action => 'list', :order => 

‘created_on’ %>

<%= link_to ‘Category’, :action => ‘list’, :order =>
‘category_id’ %>

This doesn’t allow switching between ASC and DESC either. plus I feel
that it could be automagic to some degree?

Oh, forgot this part:
def list
@item_pages, @items = paginate(:items,
:per_page => 10,
:order_by => (params[:order]))
end

Will Jessup wrote:

Oh, forgot this part:
def list
@item_pages, @items = paginate(:items,
:per_page => 10,
:order_by => (params[:order]))
end

Does it hold the order when you press ‘next page’? Problem I’ve got is
that I can order the table page that is displayed, but as soon as I
press ‘next page’ the order is not sorted. I am looking for the ‘rails
way’ to do this.

Jeff Gordon wrote:

Will Jessup wrote:

Oh, forgot this part:
def list
@item_pages, @items = paginate(:items,
:per_page => 10,
:order_by => (params[:order]))
end

Does it hold the order when you press ‘next page’? Problem I’ve got is
that I can order the table page that is displayed, but as soon as I
press ‘next page’ the order is not sorted. I am looking for the ‘rails
way’ to do this.

I havn’t even gotten that far =]

Ok this is just my rails day 3 but in my PHP pages I stored the sort
order in the session for every table… You would have something like

session[:your_table_order] = params[:order] || your_default_order

and in paginate, you’ll have to :order_by => session[:your_table_order]

You get the order from a GET or POST parameter, the table will only be
sorted if this parameter is present. If you look at the links for “next
page” and “previous page” you will notice that they don’t send this
order parameter --> The next page won’t be ordered. I’m quite sure there
is a way to do this without sessions…

Please correct me, if I’m wrong.

Is this secure, or should params[:order] be validated against acceptable
values first?

Will Jessup wrote:

Oh, forgot this part:
def list
@item_pages, @items = paginate(:items,
:per_page => 10,
:order_by => (params[:order]))
end

I dont see why it would be insecure. If someone happened to type a non
existing column name in there they would (should) get an error back. I
don’t
think there is much you can do to screw up the process in the order
clause.
And this is also the way I do it as well.

I also preform one more check on the parameters to see it is already in
the
session, if it is I switch it between ASC or DESC depending on which is
already set. My code for this is a little messy right now, but I could
most
certainly go back and clean it up if I’m ever that area again.

-Nick

There is a SortHelper that you may use as a starting point
http://wiki.rubyonrails.com/rails/pages/Sort+Helper