Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
sorting on table columns
6 Posts
sorting on table columns


As I see this is simple.

1) Attach a hyperlink to your table column headers (html) passing the column name as the parameter. (list.rhtml) <table>

<th><a href="?sort_by=name">Name</a></th> .. .. </table>

2) In your controller add the following to the method for listing your records (list action)

def list sort_by = params[:sort_by] sort_by ||= " name" # for default sort @users = User.find(:user_search_query, ::order => sort_by) # you will need the :include option here to join the roles table


See how simple is that.

hi pls refer to following link http://dev.nozav.org/rails_ajax_table.html incase of any more queris, http://saurabh.purnye.googlepages.com

Hi, It is working fine.

But in my case i wanted to display the items in sorted order from different tables.

is there any way to get the values sorted from objects(not from the tables)?

Ex: I have a users and roles table. @user_roles = some query result (values from both the tables)

any way to perform sort operation on @user_roles instead of User and Role table?

Please let us know, any one knew this.

Thanks, Balu.

Thanks Alex.

will look into that.



As i know, guys from ELC did plugin for that: http://www.elctech.com/2007/5/8/sortable-column-headers




Here is my problem.

I have user table with fields name, role and login_time.

Same way i am displaying those 3 fields in a

in view as below.

I wanted to implement sort operation for each field in the table.

How can i implement the same in Ruby On Rails?, any example code or any link to go through?

Please share some information on this.

Thanks, Balu.

6 Posts
Login to add your message