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 T. 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.

i want to know more about sorting in ruby.i hav project and
customer model.In my project listing i want to sort the table based
on the column heading .so i want a method having column headings that
when you click
on them, sort view according to that column heading?.In my view i hav
proj-id,customer name,location.start date etc…so many fields.
I want to sort the entire tsble based on customer name.i want a detail
explanation that how should my controller and view look like…how can
i?can you help me.

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.

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

Check out Ryan B. Railcasts on the subject… here’s one that should
give you some ideas:

http://railscasts.com/episodes/15

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs