sorting columns in my view

What's the current method for having column headings that when you click
on them, sort your view according to that column heading? I used the
sortable_columns plug in in the past, but it seems a force-fit for Rails
2.0 respond_to ... format.html

Jim Tobin wrote:

What's the current method for having column headings that when you click
on them, sort your view according to that column heading? I used the
sortable_columns plug in in the past, but it seems a force-fit for Rails
2.0 respond_to ... format.html

I decided to use a helper method when I did this recently to account for
columns which would be sorted on a related model's value, rather than
the id in the column. The helper I am using is:

  def project_sort_header column_name, display_as = nil
    display_as ||= column_name.to_s.humanize
    link_to display_as, project_list_path(:sort => column_name)
  end

In my view, I have table headings defined thusly:

    <th><%= project_sort_header 'groups.name', 'Group' %></th>
    <th><%= project_sort_header :title %></th>
    <th>Description</th>
    <th><%= project_sort_header :priority %></th>
    <th><%= project_sort_header 'users.username', 'Owner' %></th>

And in the controller:

  order = params[:sort] || :priority
  @projects = Project.find ;all, :order => order, :include => ...

For multiple models, the helpers can be DRYd by passing in the table
type and using self.send and constructing the xxx_list_path method from
the table type.

jokrish wrote:

i want to know more about sorting in ruby.

Sorting is handled by your call to the model's find method; so to make
it user selectable, you use a conditions hash with the find method so
you can insert the field name the user clicked on into the find
conditions.

http://api.rubyonrails.org/classes/ActiveRecord/Base.html

The explanation of Conditions is the second section on that page.

Check out Ryan Bates Railcasts on the subject... here's one that should
give you some ideas:

http://railscasts.com/episodes/15