# Sorting helper does not work -- need help

**URL:** <https://www.ruby-forum.com/t/sorting-helper-does-not-work-need-help/135337>\
**Category:** Rails\
**Created:** [April 21, 2008, 10:03am UTC](https://www.ruby-forum.com/t/sorting-helper-does-not-work-need-help/135337 "2008-04-21T10:03:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![durgesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@durgesh](https://www.ruby-forum.com/u/durgesh)\
**Post date:** [April 21, 2008, 10:03am UTC](https://www.ruby-forum.com/t/sorting-helper-does-not-work-need-help/135337/1 "2008-04-21T10:03:28Z")

</div>

Hi guys!

I am still not able to get the sorting onclick of the table headers  
to work…

I get this error:

NoMethodError in PeopleController#index

You have a nil object when you didn’t expect it!  
You might have expected an instance of ActiveRecord::Base.  
The error occurred while evaluating nil.[]

RAILS\_ROOT: C:/ruby/contact  
Application Trace | Framework Trace | Full Trace

app/helpers/sort\_helper.rb:104:in `sort' app/controllers/people_controller.rb:19:in `index’  
app/controllers/people\_controller.rb:18:in `sort' app/controllers/people_controller.rb:18:in `index’

This is my code:

# controller.rb

helper :sort  
include SortHelper

# GET /people

# GET /people.xml

def index  
# @sorter = SortingHelper::Sorter.new self, %w(name address),  
@params[‘sort’], @params[‘order’], ‘name’, ‘ASC’

```
SortHelper.columns = %w[
 name
 address

```

]  
SortHelper.default\_order = %w[name address]

# @people = Person.search(params[:search], params[:page])

@people = Person.find(:all).sort do |a, b|  
SortHelper.sort(a, b, @params)  
end

# @people = Person.find(:all)

```
#respond_to do |format|
 # format.html # index.html.erb
  #format.xml { render :xml => @people }
#end

```

end

# index.html.erb

# Listing people

\<% for person in @people %\>

\<% end %\>

| \<%= link\_to\_sort\_by 'Name', 'name' %\> | \<%= link\_to\_sort\_by 'Address', 'address' %\> |
| --- | --- |
| \<%=h person.name %\> | \<%=h person.address %\> | \<%= link\_to 'Show', person %\> | \<%= link\_to 'Edit', edit\_person\_path(person) %\> | \<%= link\_to 'Destroy', person, :confirm =\> 'Are you sure?', :method =\> :delete %\> |

  

\<%= will\_paginate @people %\>

\<%= link\_to ‘New person’, new\_person\_path %\>

# sort\_helper.rb

# = sort\_helper.rb - SortHelper for Ruby on Rails

# 

# Author: Dave B. [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

# 

# Version: 17 Feb 2005

# 

# Makes column headings users can click to define how the table is

sorted.

# 

# == Example

# === View

# 

# 

# \<%= link\_to\_sort\_by ‘First Name’, ‘name’ %\>

# \<%= link\_to\_sort\_by ‘Surname’, ‘family.name’ %\>

# \<%= link\_to\_sort\_by ‘Email’, ‘email’ %\>

# 

# 

# === Controller:

# 

# helper :sort

# 

# def list # action

# SortHelper.columns = %w[

# name

# family.name

# email

# ]

# SortHelper.default\_order = %w[family.name name]

# @people = Person.find\_all.sort do |a, b|

# SortHelper.sort(a, b, @params)

# end

# end

# 

module SortHelper

# Create a link (‘a’ tag) back to the current action, but sorting by

# _sort\_column_ before any existing ordering.

# 

# Example:

# On an un-sorted page,

# \<%= link\_to\_sort\_by ‘First Name’, ‘name’ %\>

# \<%= link\_to\_sort\_by ‘Surname’, ‘family.name’ %\>

# \<%= link\_to\_sort\_by ‘Email’, ‘email’ %\>

# could result in:

# [First Name](https://www.ruby-forum.com/person/list?sort=1)

# [Surname](https://www.ruby-forum.com/person/list?sort=2)

# [Email](https://www.ruby-forum.com/person/list?sort=3)

# 

# If the page was already sorted by first name,

# \<%= link\_to\_sort\_by ‘First Name’, ‘name’ %\>

# \<%= link\_to\_sort\_by ‘Surname’, ‘family.name’ %\>

# \<%= link\_to\_sort\_by ‘Email’, ‘email’ %\>

# could result in:

# [First Name](https://www.ruby-forum.com/person/list?sort=-1)

# [Surname](https://www.ruby-forum.com/person/list?sort=2+1)

# [Email](https://www.ruby-forum.com/person/list?sort=3+1)

# 

def link\_to\_sort\_by(link\_text, sort\_column)

```
sort_key = @@sort_keys[sort_column]

sort = (@params['sort'] || '').split.map {|param| param.to_i }

if sort[0] && sort[0].abs == sort_key
  sort[0] = -sort[0]
else
  sort.delete_if {|key| key.abs == sort_key }
  sort.unshift sort_key
end

link_to link_text, :params => {'sort' => sort.join(' ')}

```

end

# Set the columns that may be used to sort by.

# You must set this from the controller before you can use

# +link\_to\_sort\_by+ in a view.

# 

def self.columns=(column\_names)  
# prepend id so that the first sortable column is at index 1  
@@sort\_columns = [‘id’] + column\_names  
@@sort\_keys = {}  
@@sort\_columns.each\_with\_index {|obj, i| @@sort\_keys[obj] = i }  
end

# Set the columns that are used to sort by default

# 

def self.default\_order=(column\_names)  
@@default\_columns = column\_names.map {|column|  
@@sort\_keys[column] }  
end

# This is a comparison method that returns -1, 0 or 1

# depending on the passed objects _a_ and _b_, and the sorting

# priorities defined in _params_. It can be used in blocks

# given to +Enumerable#sort+.

# 

# Use it like this:

# @people = Person.find\_all.sort do |a, b|

# SortHelper.sort(a, b, @params)

# end

# 

def self.sort(a, b, params)  
if /\d/ === params[‘sort’]  
params[‘sort’].split  
else  
@@default\_columns  
end.each do |column\_index|  
column\_index = column\_index.to\_i  
next if column\_index.abs \>= @@sort\_columns.size  
a\_col = a  
b\_col = b  
@@sort\_columns[column\_index.abs].split(‘.’).each do |meth|  
a\_col = a\_col.send(meth)  
b\_col = b\_col.send(meth)  
end  
reverse = (column\_index \< 0)  
case a\_col && a\_col \<=\> b\_col  
when -1  
return reverse ? 1 : -1  
when 1, nil # nil \< anything else  
return reverse ? -1 : 1  
end  
end  
0  
end

end

Please can anyone tell me whats the mistake? I had already got the  
pagination working before, however, after i put the sorting code, it  
too is not working…

---

<div class="post-metadata">

**Author:** ![durgesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@durgesh](https://www.ruby-forum.com/u/durgesh)\
**Post date:** [April 21, 2008, 10:10am UTC](https://www.ruby-forum.com/t/sorting-helper-does-not-work-need-help/135337/2 "2008-04-21T10:10:11Z")

</div>

> # controller.rb
> 
> helper :sort

# include SortHelper

> 

try to comment include SortHelper, tell me what’s going on?

reinhart  
[http://teapoci.blogspot.com](http://teapoci.blogspot.com)

---

<div class="post-metadata">

**Author:** ![durgesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@durgesh](https://www.ruby-forum.com/u/durgesh)\
**Post date:** [April 21, 2008, 10:54am UTC](https://www.ruby-forum.com/t/sorting-helper-does-not-work-need-help/135337/3 "2008-04-21T10:54:38Z")

</div>

well, after commenting, i get the same error…

On Apr 21, 9:10 am, Visit Indonesia 2008 \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![durgesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@durgesh](https://www.ruby-forum.com/u/durgesh)\
**Post date:** [April 21, 2008, 11:14am UTC](https://www.ruby-forum.com/t/sorting-helper-does-not-work-need-help/135337/4 "2008-04-21T11:14:30Z")

</div>

Bro you run it in PeopleController or PersonController, please check it  
back because i see your script is person not people in your commented  
code for view in helper. And also check your config/route.rb to ensure  
your controller name.

Reinhart

---

<div class="post-metadata">

**Author:** ![durgesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@durgesh](https://www.ruby-forum.com/u/durgesh)\
**Post date:** [April 21, 2008, 10:56am UTC](https://www.ruby-forum.com/t/sorting-helper-does-not-work-need-help/135337/5 "2008-04-21T10:56:24Z")

</div>

well, i get the same error even after commenting the code

On Apr 21, 9:10 am, Visit Indonesia 2008 \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![durgesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@durgesh](https://www.ruby-forum.com/u/durgesh)\
**Post date:** [April 21, 2008, 12:58pm UTC](https://www.ruby-forum.com/t/sorting-helper-does-not-work-need-help/135337/6 "2008-04-21T12:58:25Z")

</div>

yes, i confirmed, it is peoplecontroller, and i have used  
peoplecontroller itself, yet i am getting the error…what could be  
the mistake?

On Apr 21, 10:14 am, Visit Indonesia 2008 \<rails-mailing-l…@andreas-
